Skip to content

Instantly share code, notes, and snippets.

@akshaydk
Created October 28, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akshaydk/5b6818e13b4e38245e29 to your computer and use it in GitHub Desktop.
Save akshaydk/5b6818e13b4e38245e29 to your computer and use it in GitHub Desktop.
#PA2-Q1
def quick_sort(A,low,high):
if low<high:
i=low+1
pivot=low
print 'pivot=', A[pivot]
left=right=count=0
for j in range(low+1,high):
# print 'i=',i
# print 'j=',j
count+=1
if A[pivot]>A[j]:
swap(A,i,j)
i+=1
#print A
swap(A,low,i-1)
print 'end',A
print 'count=',count
left=quick_sort(A,low,i-1)
right=quick_sort(A,i,high)
return left+right+count
else:
return 0
def swap(A,x,y):
temp=A[x]
A[x]=A[y]
A[y]=temp
A=[3,8,2,5,1,4,7,6,10,9]
print quick_sort(A,0,len(A))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment