Created
March 12, 2022 16:11
-
-
Save jamescasia/9a149049da4d15d8b28be5a249f65f32 to your computer and use it in GitHub Desktop.
Quick sort algorithm
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(arr): | |
if len(arr) == 1: | |
return arr | |
if len(arr) == 0: | |
return [] | |
pivot = arr[-1] | |
left = [] | |
right = [] | |
for a in arr[:-1]: | |
if a > pivot: | |
right.append(a) | |
else: | |
left.append(a) | |
left = quicksort(left) | |
right = quicksort(right) | |
return left+[pivot]+right | |
arr = [5, 2, 4, 3, 1, 8, 7, 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment