Skip to content

Instantly share code, notes, and snippets.

@reciosonny
Created June 1, 2017 10:43
Show Gist options
  • Save reciosonny/fa7346e92edbe92b1d65d9d4c252c675 to your computer and use it in GitHub Desktop.
Save reciosonny/fa7346e92edbe92b1d65d9d4c252c675 to your computer and use it in GitHub Desktop.
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