PYTHON ONLY (Execution time for sorting) Write a program that obtains the execution time of selection sort, bubble sort, merge sort, quick sort, heap sort, and radix sort for input size 50000, 100,000, 150,000, 200,000, 250,000, and 300,000. Your program should create data randomly and print a table like this:
The text gives a recursive quick sort. Write a nonrecursive version in this exercise.
Show transcribed image textAraysize Selection SortBubble So Merge Sort Quick Sort Heap So Radix Sort 50000 100000 150000 200000 250000 300000
Expert Answer
import random
from datetime import datetime
myList= random.sample(range(50000), 50000);
myList2= random.sample(range(100000), 100000);
myList3= random.sample(range(150000), 150000);
myList4= random.sample(range(200000), 200000);
myList5= random.sample(range(250000), 250000);
myList6= random.sample(range(300000), 300000);
#myList= random.sample(range(500), 500);
#myList2= random.sample(range(1000), 1000);
#myList3= random.sample(range(1500), 1500);
#myList4= random.sample(range(2000), 2000);
#myList5= random.sample(range(2500), 2500);
#myList6= random.sample(range(3000), 3000);
def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp
#1. Begin SELECTION SORT#
def selectionsort( aList ):
for i in range( len( aList ) ):
least = i
for k in range( i + 1 , len( aList ) ):
if aList[k] < aList[least]:
least = k
swap(aList,least,i )
#End SELECTION SORT#
#2. Begin BUBBLE SORT#
def bubblesort(aList):
end=len(aList)-1
while (end!=-1):
swapped=-1
for i in range(0,end):
if aList[i]>aList[i+1]:
temp=aList[i]
aList[i]=aList[i+1]
aList[i+1]=temp
swapped=i
end=swapped
#End BUBBLE SORT#
#3. Begin MERGE SORT#
def _mergesort( aList, first, last ):
mid = int( (first+last)/2)
if first < last:
_mergesort( aList, first, mid )
_mergesort( aList, mid + 1, last )
a, f, l = 0, first, mid + 1
tmp = [None] * ( last – first + 1 )
while f <= mid and l <= last:
if aList[f] < aList[l] :
tmp[a] = aList[f]
f += 1
else:
tmp[a] = aList[l]
l += 1
a += 1
if f <= mid :
tmp[a:] = aList[f:mid + 1]
if l <= last:
tmp[a:] = aList[l:last + 1]
a = 0
while first <= last:
aList[first] = tmp[a]
first += 1
a += 1
return aList
def mergesort(aList):
aList = _mergesort( aList, 0, len( aList ) – 1 )
return aList
#End MERGE SORT#
#4. Begin QUICK SORT#
def quicksort(myLst, start, end):
if start < end:
# partition the list
pivot = partition(myLst, start, end)
# sort both halves
quicksort(myLst, start, pivot-1)
quicksort(myLst, pivot+1, end)
return myLst
def partition(myLst, start, end):
pivot = myLst[start]
left = start+1
right = end
done = False
while not done:
while left <= right and myLst[left] <= pivot:
left = left + 1
while myLst[right] >= pivot and right >=left:
right = right -1
if right < left:
done= True
else:
# swap places
temp=myLst[left]
myLst[left]=myLst[right]
myLst[right]=temp
# swap start with myList[right]
temp=myLst[start]
myLst[start]=myLst[right]
myLst[right]=temp
return right
#End QUICK SORT#
#5. Begin HEAP SORT#
def heapsort( aList ):
# convert aList to heap
length = int(len( aList )) – 1
leastParent = int(length / 2)
for i in range ( leastParent, -1, -1 ):
moveDown( aList, i, length )
# flatten heap into sorted array
for i in range ( length, 0, -1 ):
if aList[0] > aList[i]:
swap( aList, 0, i )
moveDown( aList, 0, i – 1 )
def moveDown( aList, first, last ):
largest = 2 * first + 1
while largest <= last:
# right child exists and is larger than left child
if ( largest < last ) and ( aList[largest] < aList[largest + 1] ):
largest += 1
# right child is larger than parent
if aList[largest] > aList[first]:
swap( aList, largest, first )
# move down to largest child
first = largest;
largest = 2 * first + 1
else:
return # force exit
#End HEAP SORT#
#6. Begin RADIX SORT#
def radixsort(aList):
RADIX = 10
maxLength = False
tmp , placement = -1, 1
while not maxLength:
maxLength = True
# declare and initialize buckets
buckets = [list() for _ in range( RADIX )]
# split aList between lists
for i in aList:
tmp = int(i / placement)
buckets[tmp % RADIX].append(i)
if maxLength and tmp > 0:
maxLength = False
# empty lists into aList array
a = 0
for b in range( RADIX ):
buck = buckets[b]
for i in buck:
aList[a] = i
a += 1
# move to next digit
placement *= RADIX
#End RADIX SORT#
t0 = datetime.now()
selectionsort(myList);
t1 = datetime.now()
rt1 = t1-t0;
t0 = datetime.now()
selectionsort(myList2);
t1 = datetime.now()
rt2 = t1-t0;
t0 = datetime.now()
selectionsort(myList3);
t1 = datetime.now()
rt3 = t1-t0;
t0 = datetime.now()
selectionsort(myList4);
t1 = datetime.now()
rt4 = t1-t0;
t0 = datetime.now()
selectionsort(myList5);
t1 = datetime.now()
rt5 = t1-t0;
t0 = datetime.now()
selectionsort(myList6);
t1 = datetime.now()
rt6 = t1-t0;
t0 = datetime.now()
bubblesort(myList);
t1 = datetime.now()
rt7 = t1-t0;
t0 = datetime.now()
bubblesort(myList2);
t1 = datetime.now()
rt8 = t1-t0;
t0 = datetime.now()
bubblesort(myList3);
t1 = datetime.now()
rt9 = t1-t0;
t0 = datetime.now()
bubblesort(myList4);
t1 = datetime.now()
rt10 = t1-t0;
t0 = datetime.now()
bubblesort(myList5);
t1 = datetime.now()
rt11 = t1-t0;
t0 = datetime.now()
bubblesort(myList6);
t1 = datetime.now()
rt12 = t1-t0;
t0 = datetime.now()
mergesort(myList);
t1 = datetime.now()
rt13 = t1-t0;
t0 = datetime.now()
mergesort(myList2);
t1 = datetime.now()
rt14 = t1-t0;
t0 = datetime.now()
mergesort(myList3);
t1 = datetime.now()
rt15 = t1-t0;
t0 = datetime.now()
mergesort(myList4);
t1 = datetime.now()
rt16 = t1-t0;
t0 = datetime.now()
mergesort(myList5);
t1 = datetime.now()
rt17 = t1-t0;
t0 = datetime.now()
mergesort(myList6);
t1 = datetime.now()
rt18 = t1-t0;
t0 = datetime.now()
quicksort(myList,0,int(len(myList))-1);
t1 = datetime.now()
rt19 = t1-t0;
t0 = datetime.now()
quicksort(myList2,0,int(len(myList))-1);
t1 = datetime.now()
rt20 = t1-t0;
t0 = datetime.now()
quicksort(myList3,0,int(len(myList))-1);
t1 = datetime.now()
rt21 = t1-t0;
t0 = datetime.now()
quicksort(myList4,0,int(len(myList))-1);
t1 = datetime.now()
rt22 = t1-t0;
t0 = datetime.now()
quicksort(myList5,0,int(len(myList))-1);
t1 = datetime.now()
rt23 = t1-t0;
t0 = datetime.now()
quicksort(myList6,0,int(len(myList))-1);
t1 = datetime.now()
rt24 = t1-t0;
t0 = datetime.now()
heapsort(myList);
t1 = datetime.now()
rt25 = t1-t0;
t0 = datetime.now()
heapsort(myList2);
t1 = datetime.now()
rt26 = t1-t0;
t0 = datetime.now()
heapsort(myList3);
t1 = datetime.now()
rt27 = t1-t0;
t0 = datetime.now()
heapsort(myList4);
t1 = datetime.now()
rt28 = t1-t0;
t0 = datetime.now()
heapsort(myList5);
t1 = datetime.now()
rt29 = t1-t0;
t0 = datetime.now()
heapsort(myList6);
t1 = datetime.now()
rt30 = t1-t0;
t0 = datetime.now()
radixsort(myList);
t1 = datetime.now()
rt31 = t1-t0;
t0 = datetime.now()
radixsort(myList2);
t1 = datetime.now()
rt32 = t1-t0;
t0 = datetime.now()
radixsort(myList3);
t1 = datetime.now()
rt33 = t1-t0;
t0 = datetime.now()
radixsort(myList4);
t1 = datetime.now()
rt34 = t1-t0;
t0 = datetime.now()
radixsort(myList5);
t1 = datetime.now()
rt35 = t1-t0;
t0 = datetime.now()
radixsort(myList6);
t1 = datetime.now()
rt36 = t1-t0;
print (“AnalysistSelectiontBubbletMergetQuicktHeaptRadix”)
print (“50000 t”,rt1.microseconds/1000,”t”,rt7.microseconds/1000,”t”,rt13.microseconds/1000,”t”,rt19.microseconds/1000,””,rt25.microseconds/1000,””,rt31.microseconds/1000);
print (“100000 t”,rt2.microseconds/1000,”t”,rt8.microseconds/1000,””,rt14.microseconds/1000,”t”,rt20.microseconds/1000,””,rt26.microseconds/1000,””,rt32.microseconds/1000);
print (“150000 t”,rt3.microseconds/1000,”t”,rt9.microseconds/1000,”t”,rt15.microseconds/1000,””,rt21.microseconds/1000,””,rt27.microseconds/1000,””,rt33.microseconds/1000);
print (“200000 t”,rt4.microseconds/1000,”t”,rt10.microseconds/1000,”t”,rt16.microseconds/1000,””,rt22.microseconds/1000,””,rt28.microseconds/1000,””,rt34.microseconds/1000);
print (“250000 t”,rt5.microseconds/1000,”t”,rt11.microseconds/1000,”t”,rt17.microseconds/1000,””,rt23.microseconds/1000,””,rt29.microseconds/1000,””,rt35.microseconds/1000);
print (“300000 t”,rt6.microseconds/1000,”t”,rt12.microseconds/1000,”t”,rt18.microseconds/1000,””,rt24.microseconds/1000,””,rt30.microseconds/1000,””,rt36.microseconds/1000);