Naïve Quicksort implementation in Python inspired by Computerphile's Quicksort in 5 lines of code
Last active
January 27, 2025 10:52
-
-
Save walkermatt/ce202a67ee4a01c3d885b652063185b6 to your computer and use it in GitHub Desktop.
Naïve Quicksort implementation in Python
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 qs(l): | |
if len(l) == 0: | |
return l | |
n = l.pop(0) | |
# print(f'n: {n}') | |
smaller = [x for x in l if x <= n] | |
larger = [x for x in l if x > n] | |
# print(f'smaller: {smaller}') | |
# print(f'larger: {larger}') | |
return qs(smaller) + [n] + qs(larger) | |
if __name__ == '__main__': | |
print(qs([7,4,5,9,3,2,1,6,8,0])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment