Last active
May 14, 2018 12:49
-
-
Save yigitozgumus/d1354d3c87f5aed4c68775be60b7a557 to your computer and use it in GitHub Desktop.
Quicksort class implementation
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): | |
p = 0 | |
r = len(arr) - 1 | |
solve_recursive(arr,p,r) | |
def solve_recursive(arr,p,r): | |
if(p<r): | |
q = partition(arr,p,r) | |
solve_recursive(arr,p,q-1) | |
solve_recursive(arr,q+1,r) | |
def partition(arr,p,r): | |
x = arr[r] | |
i = p - 1 | |
for j in range(p,r): | |
if(arr[j] <= x): | |
i = i + 1 | |
arr[i],arr[j] = arr[j],arr[i] | |
arr[i+1],arr[r] = arr[r],arr[i+1] | |
return i+1 | |
def quicksort_m2(arr): | |
p = 0 | |
r = len(arr) - 1 | |
solve_tail_recursive(arr,p,r) | |
def solve_tail_recursive(arr,p,r): | |
while(p<r): | |
q = partition(arr,p,r) | |
solve_tail_recursive(arr,p,q-1) | |
p += q+1 | |
def main(): | |
arr = [2,4,3,6,5,8,6,7,1,9,14,32,30,23,87,43] | |
print("before sort :", arr) | |
quicksort(arr) | |
print("sorted :",arr) | |
if __name__ == "__main__" : | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment