Skip to content

Instantly share code, notes, and snippets.

@rohitdholakia
Last active August 29, 2015 13:57
Show Gist options
  • Save rohitdholakia/9731540 to your computer and use it in GitHub Desktop.
Save rohitdholakia/9731540 to your computer and use it in GitHub Desktop.
Partition algorithm
def partition(a, lo, hi):
pivot = a[lo]
left = lo + 1
right = hi
while left <= right:
while a[left] < pivot:
left += 1
while a[right] > pivot:
right -= 1
if left <= right:
a[left], a[right] = a[right], a[left]
left += 1
right -= 1
a[lo], a[right] = a[right], a[lo]
return right
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment