Skip to content

Instantly share code, notes, and snippets.

@jackhooper
Last active May 8, 2022 03:48
Show Gist options
  • Save jackhooper/8208305 to your computer and use it in GitHub Desktop.
Save jackhooper/8208305 to your computer and use it in GitHub Desktop.
Quicksort in Python. Functional-programming style, zero data mutation.
# Recursive quicksort algorithm implemented in Python
# FP-style, with no data mutation whatsoever
# One major advantage of not mutating the data is that if you throw an immutable
# data structure at it - such as a tuple or a string - it won't blow up in your face.
# Jack Hooper 2014
def quicksort(array):
if len(array) <= 1:
return array
higher = [i for i in array[1:] if i > array[0]]
lower = [i for i in array[1:] if i <= array[0]]
return quicksort(lower) + [array[0]] + quicksort(higher)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment