Skip to content

Instantly share code, notes, and snippets.

@logsv
Created January 26, 2019 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logsv/60efabd32cd5e498afa1dac80b32e384 to your computer and use it in GitHub Desktop.
Save logsv/60efabd32cd5e498afa1dac80b32e384 to your computer and use it in GitHub Desktop.
In-Place QuickSort in Python
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