Created
March 5, 2012 11:22
-
-
Save fcostin/1977924 to your computer and use it in GitHub Desktop.
linear time partition
This file contains 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 exchange(a, i, j): | |
a[i], a[j] = a[j], a[i] | |
def partition(a, x): | |
n = len(a) | |
i = 0 | |
j = 0 | |
k = n | |
while j < k: | |
if a[j] < x: | |
exchange(a, i, j) | |
i += 1 | |
j += 1 | |
elif a[j] == x: | |
j += 1 | |
else: | |
k -= 1 | |
exchange(a, j, k) | |
return slice(0, i), slice(i, k), slice(k, n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment