Skip to content

Instantly share code, notes, and snippets.

@fcostin
Created March 5, 2012 11:22
Show Gist options
  • Save fcostin/1977924 to your computer and use it in GitHub Desktop.
Save fcostin/1977924 to your computer and use it in GitHub Desktop.
linear time partition
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