Created
December 30, 2016 05:41
-
-
Save robbie01/ed1452f6a0d9d0019fa825663702d240 to your computer and use it in GitHub Desktop.
Quicksort implemented in Python 3
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
from random import randrange | |
def qsort(list): | |
if len(list) > 1: | |
pivot = list.pop(randrange(0, len(list))) | |
a = qsort([i for i in list if i < pivot]) | |
b = qsort([i for i in list if i >= pivot]) | |
return a + [pivot] + b | |
else: | |
return list | |
if __name__ == '__main__': | |
from sys import argv | |
from random import shuffle | |
list = list(range(int(argv[1]))) | |
shuffle(list) | |
list = qsort(list) | |
print(all(list[i] <= list[i+1] for i in range(len(list)-1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment