Last active
December 21, 2015 05:39
-
-
Save cpdean/6258721 to your computer and use it in GitHub Desktop.
modelled after haskell's textbook quicksort
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
def quicksort(bros): | |
if len(bros) == 0: | |
return [] | |
else: | |
first = bros[0] | |
rest = bros[1:] | |
smallerbros = quicksort([i for i in rest if i < first]) | |
biggerbros = quicksort([i for i in rest if i >= first]) | |
return smallerbros + [first] + biggerbros | |
e = [1, 2, 3, 4, 5] | |
def test_1(): | |
assert quicksort(e) == e | |
def test_2(): | |
a = [5, 4, 3, 2, 1] | |
assert quicksort(a) == e | |
def test_3(): | |
a = [4, 5, 3, 1, 2] | |
assert quicksort(a) == e | |
if __name__ == '__main__': | |
test_1() | |
test_2() | |
test_3() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment