### Sorting algorithms and timings. # The timing operations of this program only work on linux. import timing import time import sys from random import * ###### bubblesort def bubble(A): print "write this one yourself" # bubble ######## Swap sort: swap the largest element to the end # find index of largest element in the array UPTO index endi def maxi(A,endi): if endi<0 or endi>=len(A): raise "parameter error" # error check mi = 0 # the index to be returned i = 1 # loop counter while i<= endi: if A[i] > A[mi]: mi = i i +=1 return mi # maxi def swapsort(A): # keep swapping largest value to end of array i = len(A)-1 # start at end while i>0: # last element naturally falls into place mi = maxi(A,i) A[i],A[mi] = A[mi],A[i] # swap largest to correct position # A[i], A[ maxi(A,i) ] = A[ maxi(A,i) ], A[i] # inefficient i = i-1 # swapsort ######## Quick sort (non-destructive): ############ # return index of first element out of order, -1 if none exists def ndfindpivot(A): an = -1 i =0 while iA[i+1]: an = i+1 i += 1 return an #findpivot # First version of partition is non-destructive; returns pair of arrays def ndpartition(pi,A): # parition A according to pivot at index pi pivot = A[pi] L = [] # left partition R = [] # right partition i = 0 while i pivot using two indices # index j will keep track of the end of the first partition + 1, index i will # go through the list and swap every element < pivot with j, and advance j. # This function will also return the end value of j, which marks start of # second partition. def partition(A,piv,start,end): i = start # go through A one by one. j = start # first available "slot" for the left partition. while i1: size = int(sys.argv[1]) # size of test array as command-line argument else: size = 2000 A,B,C = [],[],[] i = 0 while i