Created
March 4, 2023 12:55
-
-
Save rolangom/6acd5a92b085f5b4ce78e243c5f3db33 to your computer and use it in GitHub Desktop.
Quick sort implementation in python using recursive calls and for comprehension
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 quicksort(array): | |
if len(array) == 0: | |
return array | |
pivot = array[0] | |
rest = array[1:] | |
smallers = quicksort([x for x in rest if x < pivot]) | |
biggers = quicksort([x for x in rest if x >= pivot]) | |
return [*smallers, pivot, *biggers] | |
# for olders version of python | |
# return smallers + [pivot] + biggers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment