Skip to content

Instantly share code, notes, and snippets.

/example.py Secret

Created February 1, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4905e552e6462bee655d8b37aa0c6eaa to your computer and use it in GitHub Desktop.
Save anonymous/4905e552e6462bee655d8b37aa0c6eaa to your computer and use it in GitHub Desktop.
quick sort of Hoare partition
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