Skip to content

Instantly share code, notes, and snippets.

@Feuermurmel
Created August 21, 2016 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Feuermurmel/bfc124d9b9eb99b886eede709dda311a to your computer and use it in GitHub Desktop.
Save Feuermurmel/bfc124d9b9eb99b886eede709dda311a to your computer and use it in GitHub Desktop.
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