Skip to content

Instantly share code, notes, and snippets.

@always-hii
Created May 18, 2018 06:05
Show Gist options
  • Save always-hii/536bc15c349208bcc2416af57f60851d to your computer and use it in GitHub Desktop.
Save always-hii/536bc15c349208bcc2416af57f60851d to your computer and use it in GitHub Desktop.
Quicksort example
def partition(list, l, r):
pivot = list[r]
i=l
j=r-1
while True:
while i<r and list[i]<=pivot:
i+=1
while j>l and list[j]>pivot:
j-=1
if i<j:
list[i], list[j] = list[j], list[i]
else:
break
list[r], list[i] = list[i], list[r]
return i
def quicksort(list, l, r):
if l>=r:
return
pivot_index = partition(list, l, r)
quicksort(list, l, pivot_index-1)
quicksort(list, pivot_index+1, r)
A = [1, 3, 5, 7, 9, 8, 6, 4, 2]
quicksort(A, 0, len(A)-1)
print(str(A))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment