Question: Hi, I'm having some problems in python with bubble sort. My teacher gave us the actual code for the original bubble sort that should be

Hi, I'm having some problems in python with bubble sort.

My teacher gave us the actual code for the original bubble sort that should be used, which is:

Hi, I'm having some problems in python with bubble sort. My teacher

and the main function is as follows:

gave us the actual code for the original bubble sort that should

I thought that switching the greater than to a less than would be a simple fix, and it does do descending order, but will not do ascending order after the first iteration.

I don't understand what i'm doing wrong as I've tried several approaches to fix this, but don't undestand why it does not switch it to ascending order after doing descending.

Any help and explanations would be greatly appreciated.

For ease I've attached the code below:

import random import time

def bubbleSort(myList): """Rearranges the items in myList so they are in ascending order""" for lastUnsortedIndex in range(len(myList)-1,0,-1): for testIndex in range(lastUnsortedIndex): if myList[testIndex] > myList[testIndex+1]: temp = myList[testIndex] myList[testIndex] = myList[testIndex+1] myList[testIndex+1] = temp

def shuffle(myList): for fromIndex in range(len(myList)): toIndex = random.randint(0,len(myList)-1) temp = myList[fromIndex] myList[fromIndex] = myList[toIndex] myList[toIndex] = temp

def main(): print("bubbleSort Timings") aList = list(range(10000,0,-1)) print( " Before sorting list: ",end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) start = time.clock() bubbleSort(aList) end = time.clock() print( "sorted list:", end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) print( "Time to sort",end - start,"seconds") print( " Before sorting list: ", end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) start = time.clock() bubbleSort(aList) end = time.clock() print( "sorted list:", end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) print( "Time to sort",end - start,"seconds") aList = list(range(10000,0,-1)) shuffle(aList) print( " Before sorting (random) list: ",end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) start = time.clock() bubbleSort(aList) end = time.clock() print( "sorted list:",end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) print( "Time to sort",end - start,"seconds") aList = list(range(10000,0,-1)) shuffle(aList) print( " Before sorting (random) list: ",end='') print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) start = time.clock() bubbleSort(aList) end = time.clock() print( "sorted list:",end="") print( aList[0],aList[1],aList[2], '...',aList[-3], aList[-2],aList[-1]) print( "Time to sort",end - start,"seconds") input("Hit -key to end")

if __name__ == "__main__": main()

import random import time def bubbleSort (myList) """Rearranges the items in myList so they are in ascending order""" for lastUnsortedIndex in range (len (myList)-1,0,-1) for testIndex in range (lastUnsortedIndex) if myList[testIndex] > myList[testIndex+1]: temp = myList [test!ndex) myList [testIndex] = myList [test!ndex+1] myList [test!ndex+1] temp = def shuffle (myList) for fromIndex in range (len (myList)) toIndex = random. randint (0,len (myList)-1) temp = myList [fromIndex) myList [fromIndex] = myList [toIndex] myList [toIndex] = temp

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!