Last active
August 29, 2015 13:57
-
-
Save rohitdholakia/9731540 to your computer and use it in GitHub Desktop.
Partition algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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