code is
def part(a, left, right): #left and right are the start and end indixes of the subarray
i = left + 1
pivot = a[left]# choose first element in subarray as pivot
j= i+1
for j in range(left + 1,right+1):
if a[j] < pivot:
a[j], a[i] = a[i], a[j]
i += 1
pos = i - 1
a[left], a[pos] = a[pos], a[left]
return pos #new pivot position. Used to divide the next left and right side of the array.
def quick_sort(a, left, right):
if left < right:
q = part(a, left, right )
quick_sort(a, left, q - 1)
quick_sort(a, q + 1, right)
comparison= right-left
totalcomparison=+comparison
print totalcomparison
if __name__=="__main__": # If this script is run as a program:
a = [6,5,4,3,2,1]
k=0
left= 0
right= len(a)-1
quick_sort(a,left,right)
print a
This post has been edited by baavgai: 14 February 2013 - 04:43 AM
Reason for edit:: tagged

New Topic/Question
Reply



MultiQuote




|