Created
January 26, 2019 15:58
-
-
Save logsv/60efabd32cd5e498afa1dac80b32e384 to your computer and use it in GitHub Desktop.
In-Place QuickSort in Python
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
N = int(raw_input().strip()) | |
ar = map(int, raw_input().strip().split()) | |
def quicksort(ar, l, h): | |
if l < h: | |
pi = partition(ar, l, h) | |
quicksort(ar,l,pi-1) | |
quicksort(ar,pi+1, h) | |
def partition(ar, l, h): | |
p = ar[h] | |
i = l - 1 | |
for j in range(l,h): | |
if ar[j] <= p: | |
i = i + 1 | |
ar[i], ar[j] = ar[j], ar[i] | |
ar[i+1], ar[h] = ar[h], ar[i+1] | |
print " ".join(map(str, ar)) | |
return (i+1) | |
quicksort(ar, 0, N-1) | |
print " ".join(map(str, ar)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment