Created
March 14, 2013 12:48
-
-
Save joshbode/5161050 to your computer and use it in GitHub Desktop.
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 collections import defaultdict | |
| def splitter(l, mapper): | |
| """Partition an iterable by a callable mapper.""" | |
| results = defaultdict(list) | |
| for x in l: | |
| results[mapper(x)].append(x) | |
| return results | |
| def qsort(l): | |
| """QuickSort an iterable.""" | |
| if len(l) <= 1: | |
| return l | |
| pivot = l[0] | |
| split = splitter(l, lambda x: cmp(x, pivot)) | |
| return qsort(split[-1]) + split[0] + qsort(split[1]) | |
| def qsort2(l): | |
| """QuickSort an iterable.""" | |
| if len(l) <= 1: | |
| return l | |
| pivot = l[0] | |
| result = [ | |
| y | |
| for partition, x in splitter(l, lambda x: cmp(x, pivot)).items() | |
| for y in (qsort2(x) if partition else x) | |
| ] | |
| return result | |
| def qsort3(l): | |
| if len(l) <= 1: | |
| return l | |
| less = [] | |
| pivots = [] | |
| more = [] | |
| pivot = l[0] | |
| for x in l: | |
| (less if x < pivot else more if x > pivot else pivots).append(x) | |
| return qsort3(less) + pivots + qsort3(more) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment