Sunday 25 May 2014

Programming Question - 2 Algorithms: Design and Analysis, Part 1 Stanford

from sys import argv

def quicksort(A,begin,end) :
count = 0
if end - begin <= 1:
return 0
else :
#swap done here
A[begin], A[end-1] = A[end-1] , A[begin]
split = partition(A,begin,end)
count = end - begin - 1
lc = quicksort(A,begin,split)
rc = quicksort(A,split+1,end)
return count + lc + rc


def partition(A,begin,end) :
pivot = A[begin]
i = begin + 1

for j in range(begin+1,end) :
if A[j] < pivot :
A[i], A[j] = A[j], A[i]
i = i + 1

A[i-1], A[begin] = A[begin], A[i-1]
return i-1


len(A)
quicksort(A,0,len(A))



Output1: 162085
Output2: 164123
Output3: 138382

No comments:

Post a Comment