1

So I am new to python and I have a project which requires us to go through a really long tuple list and we have to order the list in descending and ascending order. However, for both my functions I always get ascending order, WHAT IS WRONG? someone please help im really stressed out

def bubblesort_descending(tuple_list): j = len(tuple_list) made_swap = True swaps = 0 while made_swap: made_swap = False for cnt in range (j-1): if tuple_list[cnt] < tuple_list[cnt+1]: tuple_list[cnt], tuple_list[cnt+1] = tuple_list[cnt+1], tuple_list[cnt] made_swap = True swaps = swaps + 1 return swaps 

Main Program:

elif choice == 'd': unsorted = range(len(numbers)) shuffle(unsorted) print ("Randomised tuple list generated:") print print (unsorted) swaps = bubblesort_descending (unsorted) print print ("heres the sorted list") print print (unsorted) print print (swaps, "swap(s) made") print 
4
  • Why you no use sorted? Commented Jan 16, 2014 at 3:33
  • @thefourtheye I'm guessing this is a learning exercise. Commented Jan 16, 2014 at 3:34
  • It is sorting correctly for me, in descending order. Are you sure you posted both functions? Commented Jan 16, 2014 at 3:41
  • Yeap, its working guys I forgot to "return tuple list" lol thanks for the help though !! Commented Jan 17, 2014 at 0:34

2 Answers 2

2

The basic difference between ascending and descending sorting order is in the comparison: Here is a bubble sort implementation as taken from http://www.codecodex.com/wiki/Bubble_sort#Python:

def bubble_sort(lst, asc=True): lst = list(lst) # copy collection to list for passesLeft in range(len(lst)-1, 0, -1): for i in range(passesLeft): if asc: if lst[i] > lst[i + 1]: lst[i], lst[i + 1] = lst[i + 1], lst[i] else: if lst[i] < lst[i + 1]: lst[i], lst[i + 1] = lst[i + 1], lst[i] return lst 

Note: The difference based on the asc parameter?

Example:

>>> xs = [1, 2, 9, 4, 0] >>> bubble_sort(xs, asc=True) [0, 1, 2, 4, 9] >>> bubble_sort(xs, asc=False) [9, 4, 2, 1, 0] 

So in effect swapping your logical operator < to > reverses the sorting order.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to convert that iterator to a list.

unsorted = range(10) unsorted_list = list(unsorted) 

After this, your code will sort in descending order because you are making a swap if tuple_list[cnt] is less than tuple_list[cnt+1]. If you change the logical operator from "<"to ">" you will get ascending order because after changing, you will make swaps if tuple_list[cnt] is greater than tuple_list[cnt+1]

By naming your list as tuple_list, it is kind of confusing. Because in python lists and tuples are different.
What's the difference between lists and tuples?

7 Comments

Actually range() in Python 2 gives you a list. In Python 3 it gives you an iterator of <class 'range'>.
since he is using prints as functions (print()) i assumed he is not using python 2. So he is not creating a list. For python 3, I didn't know that. Thanks for info. Updating my answer according to that.
You cannot make that assumption. from __future__ import print_function.
"print_function 2.6.0a2 3.0 PEP 3105: Make print a function" from docs.python.org/2/library/__future__.html -- It's been in Python 2.6 for quite some time.
Ok. Another unknown. Thanks again. =) Code should work in python 2.6+ because range() would have created list. Since it is not working, it is another thing that makes me assuming he is using python 3. (seems only "right" thing)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.