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:
and the main function is as follows:
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
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] = tempStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started