-
-
Save anonymous/4905e552e6462bee655d8b37aa0c6eaa to your computer and use it in GitHub Desktop.
quick sort of Hoare partition
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 quickSort(arr): | |
def partition(arr, left, right): | |
pivot = arr[left] | |
i = left - 1 | |
j = right + 1 | |
while True: | |
while i < right: | |
i += 1 | |
if arr[i] >= pivot: | |
break | |
while left < j: | |
j -= 1 | |
if pivot >= arr[j]: | |
break | |
if i >= j: | |
return j | |
arr[i], arr[j] = arr[j], arr[i] | |
def quickSort(arr, left, right): | |
if left < right: | |
p = partition(arr, left, right) | |
quickSort(arr, left, p) | |
quickSort(arr, p + 1, right) | |
quickSort(arr, 0, len(arr) - 1) | |
if __name__ == '__main__': | |
arr = [3,7,8,5,1,2,11,5,4] | |
quickSort(arr) | |
print(arr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment