Created
August 21, 2016 11:46
-
-
Save Feuermurmel/bfc124d9b9eb99b886eede709dda311a to your computer and use it in GitHub Desktop.
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
def partition_items(items, num_top_items, key=lambda x: x): | |
if items and 0 < num_top_items < len(items): | |
pivot = items[0] | |
smaller = [] | |
larger = [] | |
for i in items[1:]: | |
if key(i) < key(pivot): | |
smaller.append(i) | |
else: | |
larger.append(i) | |
smaller = partition_items(smaller, num_top_items, key) | |
larger = partition_items(larger, num_top_items - len(smaller) - 1, key) | |
items = smaller + [pivot] + larger | |
return items |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment