Question: In Python, Using a random number generator, create a list of 500 integers. Perform a benchmark analysis using some of the sorting algorithms from this
In Python, Using a random number generator, create a list of 500 integers. Perform a benchmark analysis using some of the sorting algorithms from this chapter.(I am using bubble, merge,and selection). What is the difference in execution speed?
def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp
def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf)
i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1
while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1
while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 print("Merging ",alist)
def selectionSort(alist): for fillslot in range(len(alist)-1,0,-1): positionOfMax=0 for location in range(1,fillslot+1): if alist[location]>alist[positionOfMax]: positionOfMax = location
temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
