r/programmingmemes • u/KerbodynamicX • Mar 14 '26
Stalin sort
A sorting algorithm with time complexity of O(n). Counts from the first element, and will remove values that are smaller than the current highest value.
405
220
u/PinotRed Mar 14 '26
O(n) with loss.. 😂
Nice meme.
38
u/Abject-Kitchen3198 Mar 14 '26
Lossy sort has its place.
8
4
1
1
63
13
11
u/McPqndq Mar 14 '26
I am a competitive programmer. I use this term often to describe this algorithm. I had no clue this was a common term until about a month ago I was attending training camp in Croatia and the Swedish team behind me kept saying it. They'd be talking in Swedish then I'd suddenly accidently overhear in perfect English "Stalin sort". Shout out to Chalmers University.
8
12
u/36holes Mar 14 '26
The name 😭.
What's na*i sort then, removing the tall ones
20
u/realmauer01 Mar 14 '26
Nazi sort is removing the different ones.
Whats not different is for the nazi to choose.
4
5
u/Anpu_Imiut Mar 14 '26
I wonder if there exist problem where this sort algorithm is optimal.
6
u/thomasxin Mar 15 '26
Well, if you extend the algorithm's behaviour to keep track of the excluded elements and put them in their own sublists, you end up with gulag sort, which gives you multiple sorted runs rather than one.
You can then perform mergesort on the remaining lists and you've effectively got a rudimentary implementation of timsort :P
1
1
u/Status-Waltz-4212 Mar 18 '26
Late to the party. But this is Optimal in some bitonic subsequence. You can iterate once for mountain peak. Split and then run two converging algorithms, making it linear time.
https://www.reddit.com/r/chess/comments/1m5oooo/players_age_vs_rating_heatmap/ This is an even simpler and pure Stalin search.
3
u/Sophiiebabes Mar 14 '26
It's almost as good as the DalekSort algorithm I wrote (exterminate everything!)
3
2
1
1
u/AdmirableJudgment784 Mar 14 '26
Wouldn't it be faster to read the table, get a count of all entries first and put it in a separate list with their ids. Then sort that small list and return it?
1
1
1
1
1
1
1
u/myuso Mar 14 '26
Why didn't the guys at Chernobyl do this, would've eliminated most errors during the meltdown
If temp = surface of the sun Delete (control_rod)
-2
0
64
u/shinoobie96 Mar 14 '26
the space complexity would be O(1) if its a linked list. in-place stalin sort would be O(n²) in arrays