Skip to content

Instantly share code, notes, and snippets.

@pdu
Created December 22, 2012 07:08
Show Gist options
  • Save pdu/4357851 to your computer and use it in GitHub Desktop.
Save pdu/4357851 to your computer and use it in GitHub Desktop.
quick sort implemented in python
#!/usr/bin/python
import random
def quick_sort_(buf, left, right):
ll, rr = left, right
pivot = random.choice(buf[left : right + 1])
while left <= right:
while buf[left] < pivot:
left += 1
while buf[right] > pivot:
right -= 1
if left <= right:
buf[left], buf[right] = buf[right], buf[left]
left += 1
right -= 1
if right > ll:
quick_sort_(buf, ll, right)
if left < rr:
quick_sort_(buf, left, rr)
def quick_sort(buf):
quick_sort_(buf, 0, len(buf) - 1)
def main():
buf = [10, 9, 8, 7, 6, 20, 18, 16, 14, 12, 10]
quick_sort(buf)
print buf
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment