Skip to content

Instantly share code, notes, and snippets.

@OleksiyRudenko
Created April 28, 2020 09:34
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 OleksiyRudenko/1843e496f373498274f19eb8d0204544 to your computer and use it in GitHub Desktop.
Save OleksiyRudenko/1843e496f373498274f19eb8d0204544 to your computer and use it in GitHub Desktop.
/*
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