Created
April 28, 2020 09:34
-
-
Save OleksiyRudenko/1843e496f373498274f19eb8d0204544 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
/* | |
I came up with a single pass O(n) sort algorithm 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. | |
© 2018-10-25, @mathew@mastodon.social | |
*/ | |
const stalinSortReduce = arr => arr.reduce((acc,v) => acc.length ? acc[acc.length-1] < v ? [...acc, v] : acc : [v], []) | |
const stalinSortFilter = arr => arr.filter((v,i,a) => i ? a[i-1] < v ? true : false : true) | |
const stalinSortFilterOptimized = arr => arr.filter((v,i,a) => !i || a[i-1] < v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment