Created
February 27, 2023 01:33
-
-
Save marcelohmariano/5d4ace9efb15d3ab82e72e443bd13657 to your computer and use it in GitHub Desktop.
Quick Sort in Python
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 partition(array, low, high): | |
pi = low | |
pivot = array[high] | |
for i in range(low, high): | |
if array[i] <= pivot: | |
array[pi], array[i] = array[i], array[pi] | |
pi += 1 | |
array[pi], array[high] = array[high], array[pi] | |
return pi | |
def sort(array, low, high): | |
if low >= high: | |
return | |
pi = partition(array, low, high) | |
sort(array, low, pi - 1) | |
sort(array, pi + 1, high) | |
return array | |
def quicksort(array): | |
return sort(array, 0, len(array) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment