Skip to content

Instantly share code, notes, and snippets.

@robbie01
Created December 30, 2016 05:41
Show Gist options
  • Save robbie01/ed1452f6a0d9d0019fa825663702d240 to your computer and use it in GitHub Desktop.
Save robbie01/ed1452f6a0d9d0019fa825663702d240 to your computer and use it in GitHub Desktop.
Quicksort implemented in Python 3
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