Created
July 9, 2017 16:44
-
-
Save MrNocTV/997509b80a540d1d55b583f859e8361f to your computer and use it in GitHub Desktop.
Quick Sort
This file contains 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 partition(arr, low, high): | |
# choose pivot as | |
pivot = arr[high] | |
i = -1 | |
j = 0 | |
# move all elements that are <= pivot to the left of it | |
# and bigger elements to the right | |
while j < high: | |
if arr[j] <= pivot: | |
i += 1 | |
arr[i], arr[j] = arr[j], arr[i] | |
j += 1 | |
i += 1 | |
arr[i], arr[high] = arr[high], arr[i] | |
# return index of pivot | |
return i | |
def quick_sort(arr, low, high): | |
if low < high: | |
pivot = partition(arr, low, high) | |
# sort left side of pivot | |
quick_sort(arr, low, pivot-1) | |
# sort right side of pivot | |
quick_sort(arr, pivot+1, high) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment