Created
November 19, 2012 11:22
-
-
Save Wuvist/4110199 to your computer and use it in GitHub Desktop.
Using quicksort for shuffling an array
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 cmp(a, b): | |
i = random.randint(0, 1) | |
if i == 1: | |
return True | |
return False | |
def quicksort(q, cmp): | |
lesser=[] | |
greater=[] | |
if len(q) <= 1: | |
return q | |
for i in q[1:]: | |
if cmp(i, q[0]): | |
lesser.append(i) | |
else: | |
greater.append(i) | |
return quicksort(lesser, cmp)+q[0:1]+quicksort(greater, cmp) | |
data = range(1, 10) | |
data = quicksort(data, cmp) | |
print data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment