Last active
December 23, 2018 03:35
-
-
Save soeirosantos/5d91bbfbf2adebf28815d9282d9a7d43 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
from random import randint | |
def quick_sort(arr, start, end): | |
if start >= end: | |
return arr | |
pivot_idx = partition(arr, start, end) | |
quick_sort(arr, start, pivot_idx - 1) | |
quick_sort(arr, pivot_idx + 1, end) | |
return arr | |
def partition(arr, start, end): | |
partition_idx = randint(start, end) | |
arr[partition_idx], arr[end] = arr[end], arr[partition_idx] | |
i = start | |
for j in range(start, end): | |
if arr[j] <= arr[end]: | |
arr[i], arr[j] = arr[j], arr[i] | |
i += 1 | |
arr[i], arr[end] = arr[end], arr[i] | |
return i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment