Created
September 27, 2012 10:06
-
-
Save felipernb/3793256 to your computer and use it in GitHub Desktop.
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
import random | |
def quicksort(a, l, r): | |
if l >= r: | |
return a | |
p = partition(a, l, r) | |
quicksort(a, l, p) | |
quicksort(a, p+1, r) | |
return a | |
def partition(a, l, r): | |
p = int(random.random() * (r-l))+l | |
a[l], a[p] = a[p], a[l] | |
pivot = a[l] | |
i = l+1 | |
for j in xrange(i, r): | |
if a[j] < pivot: | |
a[j], a[i] = a[i], a[j] | |
i += 1 | |
a[l], a[i-1] = a[i-1], a[l] | |
return i-1 | |
print quicksort([5,8,2,-1,10,0,3], 0, 7) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment