-
-
Save jweinst1/a3e53bede8d6e6fd3115c2890b5701af to your computer and use it in GitHub Desktop.
Quicksort using list comprehensions
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
#source: http://en.literateprograms.org/Quicksort_(Python) | |
def qsort1(list): | |
"""Quicksort using list comprehensions""" | |
if list == []: | |
return [] | |
else: | |
pivot = list[0] | |
lesser = qsort1([x for x in list[1:] if x < pivot]) | |
greater = qsort1([x for x in list[1:] if x >= pivot]) | |
return lesser + [pivot] + greater | |
numbers = (1,6,3,32,85,23,9,123,23,336) | |
print qsort1(numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment