Created
March 28, 2012 03:13
-
-
Save jdpage/2223254 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
#!/usr/bin/python3 | |
import random | |
def sorted(l): | |
if len(l) == 0: | |
return l | |
else: | |
x, *xs = l | |
left, right = [], [] | |
for i in xs: | |
if i < x: | |
left.append(i) | |
else: | |
right.append(i) | |
sleft, sright = sorted(left), sorted(right) | |
return sleft + [x] + sright | |
l = [random.randint(1, 1000) for _ in range(100)] | |
print(l) | |
print(sorted(l)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment