Skip to content

Instantly share code, notes, and snippets.

@prasadwrites
Last active July 6, 2023 18:10
Show Gist options
  • Save prasadwrites/af213a61031ec36c1c1d to your computer and use it in GitHub Desktop.
Save prasadwrites/af213a61031ec36c1c1d to your computer and use it in GitHub Desktop.
QuickSort InPlace Python
items = [2,5,6,7,8,5,0,1,9,3,4]
#items = [3, 4, 5, 2 ,1]
def quick_sort(items,first,last):
""" Implementation of in-place quicksort """
if(first < last):
pivot, left, right = first, first, last
while(left<right):
while((left < last) and (items[left] <= items[pivot]) ):
left +=1
while((right > first) and (items[right] > items[pivot]) ):
right-=1
if(left<right):
items[left] , items[right] = items[right] , items[left]
left+=1
right-=1
items[pivot], items[right] = items[right],items[pivot]
quick_sort(items, first , right-1)
quick_sort(items, right+1, last)
print ('----------------')
print ('start %s' %items)
print ('----------------')
quick_sort(items,0,10)
print ('----------------')
print ('end %s' %items)
print ('----------------')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment