Created
June 1, 2017 10:43
-
-
Save reciosonny/8dcd3125cb892dff3b2af0fc27eff10c to your computer and use it in GitHub Desktop.
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(array): | |
if len(array) < 2: | |
return array #Base case: arrays with 0 or 1 element are already “sorted.” | |
else: | |
pivot = array[0] #Recursive case | |
less = [i for i in array[1:] if i <= pivot] #Sub-array of all the elements less than the pivot | |
greater = [i for i in array[1:] if i > pivot] #Sub-array of all the elements greater than the pivot | |
result = quicksort(less) + [pivot] + quicksort(greater) | |
print(result) | |
return result | |
print (quicksort([10, 10,5, 2, 3])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment