Created
April 28, 2021 10:36
-
-
Save harbirchahal/4c889b59fa612cb832b71714c1518ea7 to your computer and use it in GitHub Desktop.
Algo: Quick Sort
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, start, end): | |
pivot = array[start] | |
low = start + 1 | |
high = end | |
while (low <= high): | |
while (low <= end and array[low] <= pivot): | |
low += 1 | |
while (high > start and array[high] > pivot): | |
high -= 1 | |
if (low < high): | |
array[low], array[high] = array[high], array[low] | |
array[start], array[high] = array[high], array[start] | |
return high | |
def quick_sort(array, start, end): | |
if (start < end): | |
p = partition(array, start, end) | |
quick_sort(array, start, p - 1) | |
quick_sort(array, p + 1, end) | |
# Main | |
quick_sort([2, 4, 3, 1, 5], 0, 4) # [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment