Created
July 3, 2012 19:59
-
-
Save Shadow6363/3042557 to your computer and use it in GitHub Desktop.
QuickSort
This file contains hidden or 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 randint | |
def quicksort(unsorted): | |
if len(unsorted) < 2: | |
return unsorted | |
pivot, less, more = unsorted.pop((len(unsorted) - 1) / 2), [], [] | |
for elem in unsorted: | |
if elem <= pivot: | |
less.append(elem) | |
else: | |
more.append(elem) | |
return quicksort(less) + [pivot] + quicksort(more) | |
if __name__ == '__main__': | |
unsorted = [randint(0, 1000) for i in range(0, 10)] | |
print 'Random: %s\nSorted: %s' % (unsorted[:], quicksort(unsorted)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment