Last active
May 21, 2021 08:25
-
-
Save ozio/1f74b9f12eb02f93b270a442b432a001 to your computer and use it in GitHub Desktop.
StalinSort
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
/* | |
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