Created
November 16, 2011 02:19
-
-
Save g-k/1369076 to your computer and use it in GitHub Desktop.
cs188 python tutorial
This file contains hidden or 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 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