473,505 Members | 15,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

QuickSort Algorithm Problem

2 New Member
Could you please help me find the error. I myself have (i might be wrong) boiled it down to the for loop because it somehow magically converts a list into an int and tries to iterate over that. But I don't know how to fix it, doesn't even make sense why it would do it. Please help me out. Thanks in advance!

Here is the error it gives me:

TypeError: 'int' object is not iterable


Here is my code:

Expand|Select|Wrap|Line Numbers
  1. import random
  2. unsortedlist = range(0,10)
  3. random.shuffle(unsortedlist)
  4. print unsortedlist
  5.  
  6. def quickSort(ML):
  7.     if ML == []:
  8.         return []
  9.     else:        
  10.         rdex = random.randint(0, (len(ML)-1)) # chose a random index for pivot
  11.         p = ML[rdex] # get the actual pivot integer
  12.         LL, GL = [],[] 
  13.         if len(ML) == 1: 
  14.             return ML        
  15.         else:
  16.             del ML[rdex]
  17.             for i in ML:
  18.                 if i < p:
  19.                     LL.append(i)
  20.                 else:
  21.                     GL.append(i)
  22.             return quickSort(LL) + list(p) + quickSort(GL) 
  23.  
  24. if __name__ == '__main__':
  25.     print quickSort(unsortedlist)
  26.  
  27.  
Sep 6 '09 #1
3 3371
YarrOfDoom
1,247 Recognized Expert Top Contributor
What are you trying to use list() on an integer?
Sep 6 '09 #2
jollyfolly
2 New Member
I fixed it, thanks. I guess I was looking in the wrong place and made 1 wrong assumption. Because I wanted to concatenate everything into a list, and "+" only accepts lists/strings I wanted to change the int p into a list that contains an int p. Seems like I too hastily made the analogy from using simply scheme (lisp) where you can do it by saying list(x). Python does take some stuff from scheme though. But for this particular problem it looks like to achieve the same in python you have to use the square brackets around something.
Sep 6 '09 #3
Glenton
391 Recognized Expert Contributor
I haven't looked in detail at what you're writing, but the bisect module might be worth looking at?
Sep 24 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5201
by: why | last post by:
Hi, just been curious. i have learn that quicksorting algorithm is more widely used then heapsort, despite having the same performance indication of O(n log n) anyone knows why? newbie
3
2392
by: Lieven | last post by:
I want to write a generic quicksort over STL containers. We have to parallelize this algorithm afterwards. This is what I've got for now: template<typename containerIterator> void...
3
3564
by: Lieven | last post by:
I want to make a quicksort algorithm that can take both a vector and a list. This is the code I've got. But I also want to give an argument that will be used for comparing the elements. I am used...
36
5384
by: HERC777 | last post by:
just paste the below into notepad, call it iqsort.html double click it and your IE browser will run it. (neat! a universal programming language we can all run!) it falls out from the sort...
5
3013
by: Edward Hua | last post by:
Hi, I'm wondering if anybody has ever copied the quicksort algorithm from the book Numerical Recipes in C: The Art of Scientific Computing (2nd ed.), by Press, Teukolsky, Vetterling, and...
7
3310
by: zoro | last post by:
how many recursive calls will quicksort make in the worst case for a file of N items? thank you
2
15469
by: simo | last post by:
Hello to all... I am trying to write an algorithm parallel in order to realize the quicksort. They are to the first crews with C and MPI. The algorithm that I am trying to realize is PARALLEL...
8
6019
by: aparnakakkar2003 | last post by:
hello can any one tell me how i can create program to sort string list(standard template library) using quicksort.
12
4355
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.