Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
Created November 3, 2018 20:30
Show Gist options
  • Save righthandabacus/14ffd5bd1c449e57e107b9c9ee7db184 to your computer and use it in GitHub Desktop.
Save righthandabacus/14ffd5bd1c449e57e107b9c9ee7db184 to your computer and use it in GitHub Desktop.
Stalin Sort
def stalinsort(unsorted):
"""Stalin sort: O(n) sort algorithm that iterate down a
list of elements, any element which is out of order is
eliminated.
https://www.facebook.com/ProgrammersCreateLife/photos/a.241809332534619/1934139766634892/?type=1&theater
Args:
unsorted: Iterable of elements that support >= operator
Returns:
A list of subset of the input
"""
output = []
for x in unsorted:
if not output or x >= output[-1]:
output.append(x)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment