Skip to content

Instantly share code, notes, and snippets.

@ozio
Last active May 21, 2021 08:25
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 ozio/1f74b9f12eb02f93b270a442b432a001 to your computer and use it in GitHub Desktop.
Save ozio/1f74b9f12eb02f93b270a442b432a001 to your computer and use it in GitHub Desktop.
StalinSort
/*
Original idea: https://mastodon.social/@mathew
> I came up with a single pass O(n) sort algorythm I call StalinSort. You iterate down
> the list of elements checking if they're in order. Any element which is out of order
> is eliminated. At the end you have a sorted list.
*/
const stalinSort = (arr: number[]): number[] => {
let lastMax: number;
return arr.filter((item, idx) => {
if (idx === 0 || item > lastMax) {
lastMax = item;
return true;
}
return false;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment