Skip to content

Instantly share code, notes, and snippets.

@g-k
Created November 16, 2011 02:19
Show Gist options
  • Save g-k/1369076 to your computer and use it in GitHub Desktop.
Save g-k/1369076 to your computer and use it in GitHub Desktop.
cs188 python tutorial
def quicksort(L):
if len(L) < 2:
return L
pivot = L[0]
less, greater = [], []
for item in L[1:]:
(less if item < pivot else greater).append(item)
## less = [item for item in L[1:] if item < pivot]
## greater = [item for item in L[1:] if item >= pivot]
return quicksort(less) + [pivot] + quicksort(greater)
print quicksort([0, 3, 2])
print quicksort([1, 3, 2, 2, 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment