Skip to content

Instantly share code, notes, and snippets.

@zpconn
Created January 13, 2014 04:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zpconn/8394741 to your computer and use it in GitHub Desktop.
Save zpconn/8394741 to your computer and use it in GitHub Desktop.
This is a very simple recursive implementation of quicksort, mimicking the standard approach in Haskell. It's not particularly efficient, but quite elegant.
def quicksort(a):
if len(a) <= 1:
return a
lesser = [x for x in a[1:] if x < a[0]]
greater = [x for x in a[1:] if x >= a[0]]
return quicksort(lesser) + [a[0]] + quicksort(greater)
print quicksort([4, 6, 5, 3, 4, 7, 6, 5, 8, 9])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment