Skip to content

Instantly share code, notes, and snippets.

@elliotwesoff
Created February 22, 2023 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotwesoff/0b421d2591eb785cf5d9168c3059c0b2 to your computer and use it in GitHub Desktop.
Save elliotwesoff/0b421d2591eb785cf5d9168c3059c0b2 to your computer and use it in GitHub Desktop.
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