Created
June 21, 2018 05:01
-
-
Save mohapatras/cc08617a789770f4f3b927a60b4f862d to your computer and use it in GitHub Desktop.
Quick Sort implementation in python 3 with last element as pivot.
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(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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if start >= end:
return