Last active
May 8, 2022 03:48
-
-
Save jackhooper/8208305 to your computer and use it in GitHub Desktop.
Quicksort in Python. Functional-programming style, zero data mutation.
This file contains 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
# 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