Skip to content

Instantly share code, notes, and snippets.

@rohitdholakia
Last active August 29, 2015 13:57
Show Gist options
  • Save rohitdholakia/9884021 to your computer and use it in GitHub Desktop.
Save rohitdholakia/9884021 to your computer and use it in GitHub Desktop.
def three_way(a, lo, hi):
''' dutch national flag problem for 3-way partitioning in quicksort'''
key = a[random.randint(lo, hi)]
less, equal = lo
greater = hi
while equal < high:
if a[less] < key:
a[less], a[equal] = a[equal], a[less]
less += 1
equal += 1
elif a[less] == key:
equal += 1
else:
greater -= 1
a[greater], a[equal] = a[equal], a[greater]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment