Created
February 22, 2023 02:02
-
-
Save elliotwesoff/0b421d2591eb785cf5d9168c3059c0b2 to your computer and use it in GitHub Desktop.
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, left, right): | |
if left < right: | |
partition_pos = partition(arr, left, right) | |
quicksort(arr, left, partition_pos - 1) | |
quicksort(arr, partition_pos + 1, right) | |
def partition(arr, left, right): | |
i = left | |
j = right - 1 | |
pivot = arr[right] | |
while i < j: | |
while i < right and arr[i] < pivot: | |
i += 1 | |
while j > left and arr[j] >= pivot: | |
j -= 1 | |
if i < j: | |
tmp = arr[i] | |
arr[i] = arr[j] | |
arr[j] = tmp | |
if arr[i] > pivot: | |
tmp = arr[i] | |
arr[i] = arr[right] | |
arr[right] = tmp | |
return i | |
a = [3, 4, 1, 2, 1, 8, 5, 6] | |
quicksort(a, 0, len(a) - 1) | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment