Skip to content

Instantly share code, notes, and snippets.

@mohapatras
Created June 21, 2018 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohapatras/cc08617a789770f4f3b927a60b4f862d to your computer and use it in GitHub Desktop.
Save mohapatras/cc08617a789770f4f3b927a60b4f862d to your computer and use it in GitHub Desktop.
Quick Sort implementation in python 3 with last element as pivot.
def partition(A, start, end):
pivot = A[end]
pIndex = start
for i in range(start, len(A)):
if A[i] < pivot:
A[i], A[pIndex] = A[pIndex], A[i]
pIndex += 1
A[pIndex], A[end] = A[end], A[pIndex]
return pIndex
def quickSort(A, start, end):
if start == end:
return
else:
pIndex = partition(A, start, end)
quickSort(A, start, pIndex - 1)
quickSort(A, pIndex + 1, end)
if __name__ == '__main__':
A = [7,2,1,6,8,5,3,4]
start = 0
end = len(A) - 1
print("Unsorted Array: ",A)
quickSort(A, start, end)
print("Sorted Array: ",A)
@kabszac
Copy link

kabszac commented Sep 8, 2020

if start >= end:
return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment