Created
February 18, 2018 11:27
-
-
Save rahulrajpl/96635e54514d4cfc121a230c5009de10 to your computer and use it in GitHub Desktop.
Quicksort Algorithm in Python
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
import sys | |
import random | |
sys.setrecursionlimit(1500) | |
def quick_sort(A): | |
quick_sort2(A, 0, len(A)-1) | |
def quick_sort2(A, low, hi): | |
if low < hi: | |
p = partition(A, low, hi) | |
quick_sort2(A, low, p) | |
quick_sort2(A, p+1, hi) | |
def get_pivot(A, low, hi): | |
return low | |
def partition(A, low, hi): | |
pivot_index = get_pivot(A, low, hi) | |
pivot_value = A[pivot_index] | |
A[pivot_index], A[low] = A[low], A[pivot_index] | |
border = low | |
for i in range(low, hi+1): | |
if A[i] < pivot_value: | |
border += 1 | |
A[i], A[border] = A[border], A[i] | |
A[low], A[border] = A[border], A[low] | |
return border | |
if __name__ == '__main__': | |
d = random.sample(range(10),10) | |
print("Before Sorting: ", d) | |
print(quick_sort(d)) | |
print("After Quick sort ", d) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment