Skip to content

Instantly share code, notes, and snippets.

@harunyasar
Created April 26, 2017 06:06
Show Gist options
  • Save harunyasar/a17193e2dbdd3e29e193e55d2523fa6c to your computer and use it in GitHub Desktop.
Save harunyasar/a17193e2dbdd3e29e193e55d2523fa6c to your computer and use it in GitHub Desktop.
Quicksort with Python
def quicksort(a, p, r):
if p < r:
q = partition(a, p, r)
quicksort(a, p, q - 1)
quicksort(a, q + 1, r)
def partition(a, p, r):
x = a[r]
i = p - 1
for j in range(p, r):
if a[j] <= x:
i += 1
a[i], a[j] = a[j], a[i]
a[i + 1], a[r] = a[r], a[i + 1]
return i + 1
if __name__ == '__main__':
a = [11, 67, 23, 5, 101, 19, 41, 90, 2, 44, 1]
quicksort(a, 0, len(a) - 1)
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment