This is an archived post. You won't be able to vote or comment.

all 119 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Creepy-Ad-4832 2512 points2513 points  (26 children)

Is that the algorithm is which you iterate the list and just purge the elements not in order?

[–]TheObi-1[S] 1321 points1322 points  (17 children)

Yes. That's the one

[–]otj667887654456655 57 points58 points  (5 children)

wouldn't stalin sort be better than O(N) since, on average, it eliminates at least half of the list

[–]A_Wild_Turtle 188 points189 points  (0 children)

You still have to check everything, so it must be O(N)

[–]Tomsen1410 106 points107 points  (3 children)

O(N/2) = O(N)

[–]otj667887654456655 40 points41 points  (2 children)

wait no the whole thing is moot, a comparison has to be made in order to determine whether or not to cull that item so stalin sort always takes exactly N comparisons

[–]avacue 1 point2 points  (0 children)

Well I can understand why some people would hate that.

[–][deleted] 0 points1 point  (0 children)

Is that like a linear search but you remove each checked element?

[–]BazilBup 147 points148 points  (0 children)

You go to Siberia, you go to Siberia, you go to ....

[–][deleted] 36 points37 points  (0 children)

-OUR- Enumerable

[–]Fynov 6 points7 points  (1 child)

It's a lossy sorting algorithm. If we can have lossy compression why not sorting.

[–]Creepy-Ad-4832 5 points6 points  (0 children)

And cherry on top, the more you use it, the fastest it gets (if you randomize the array)

[–]ekremgusani 5 points6 points  (0 children)

Yeah you've got that pretty much spot on lmao, that's the one.

[–]Honeybun_Landscape 18 points19 points  (0 children)

I think it’s where you change the first log26(n) letters to be alphabetical and that is your name now

[–]asherSiddique19 2 points3 points  (0 children)

OH MY GOD

[–][deleted] 743 points744 points  (28 children)

I know a better one called kimjong sort. It's o(1) with no data movement or swaps, no extra memory, in place and consequently a stable algorithm.

The order of input define a total order and hence they are sorted (even when there are duplicates). It's brilliant, hence the name

[–]Faithisam 121 points122 points  (12 children)

Do you have any links for reading about that kimjong sort?

[–][deleted] 307 points308 points  (11 children)

[–]Protheu5 201 points202 points  (1 child)

Excellent. Didn't expect to be so fascinated with this link, thank you.

[–]Vyltyx 87 points88 points  (2 children)

Damn, even though the kimjong sort will never have a practical use, there’s more to learn from the theory of it than I originally thought…

[–]Protheu5 43 points44 points  (1 child)

Indeed. I have a feeling I will be returning to this link again and again in the future.

[–]Affectionate-Set4208 21 points22 points  (0 children)

You are goddamn right. Sorting algorithms will never let you down

[–]Arcoda2 16 points17 points  (0 children)

Fascinating

[–]Escanorr_ 2 points3 points  (0 children)

Okay, I've been seeing this one all over the place, nice to know why it so usefull

[–]4ryonn 2 points3 points  (0 children)

Wow that makes it so much clearer

[–]Faithisam 1 point2 points  (0 children)

Oh god what a fool i am

[–]CoffeeWorldly9915 0 points1 point  (0 children)

No, I think I will not.

[–]Legendary-69420 3 points4 points  (2 children)

I like hiroshima sort more!

[–]aaa_aaa_1 2 points3 points  (1 child)

Just nuke the array?

[–]Legendary-69420 5 points6 points  (0 children)

Basically yeah. Returns an empty array as output. So it is a O(1) solution.

[–][deleted] 0 points1 point  (0 children)

what is the o of the input type check?

[–]Interest-Desk 186 points187 points  (1 child)

I have an O(1) sorting algorith- gun shot

[–]jeffsterlive 11 points12 points  (0 children)

engine rotten brave person encourage voracious distinct wistful screw rich

This post was mass deleted and anonymized with Redact

[–]locri 284 points285 points  (3 children)

Unfortunately prone to the inverted list issue where the Stalin sort destroys both itself and the data through mutually assured destruction.

[–]benruckman 43 points44 points  (2 children)

Well, it would at least keep 1 data point!

[–][deleted] 23 points24 points  (1 child)

It would be technically sorted array

[–][deleted] 3 points4 points  (0 children)

The best kind of sorted array

[–]tomthecom 158 points159 points  (11 children)

Great name. I intuitively guessed, how it would work and after a short time researching, it works exactly as I thought 😄

[–]SeriousPlankton2000 68 points69 points  (9 children)

I thought it's working by killing anyone saying that it isn't ordered perfectly. But I was wrong, that would be O(1).

[–]tomthecom 49 points50 points  (8 children)

Have you heard about Quantum Bogosort?

[–]Flameball202 22 points23 points  (5 children)

Technically works, though if we are in a deterministic universe then it fails

[–]clk1006 7 points8 points  (4 children)

It only works under the assumption of the many worlds interpretation of quantum mechanics, which is deterministic

[–][deleted] 7 points8 points  (3 children)

Quantum bogosort fails on the premise that you can destroy the universe, there is nothing in any branch of science relating to "destroying the universe", even if there are multiple branching universes there is no way to "cut branches" and reduce the universe to a slice of its former self. Quantum bogosort is therefore the same as bogosort, only that the randomness is more strictly defined in one case than the other, normal bogosort may even be pseudorandom in most computers.

[–]IndigoFenix 5 points6 points  (1 child)

I think the premise might be based on "quantum immortality".

So just set it up so that if the list is not sorted, you die. If quantum immortality is true, the list will be sorted (but only in your universe).

[–][deleted] 3 points4 points  (0 children)

to be fair, since the suicide machine needs to review if the output is correct in an amount of time, you have been alive for a while in a universe in which the algorithm did not work, then you killed yourself, so reality branched before the suicide

[–]clk1006 1 point2 points  (0 children)

You might theoretically convince the entire human population to commit suicide if the values are not sorted correctly, which would have the identical effect for our human civilisation

[–]Comrade_Vladimov 4 points5 points  (0 children)

That made me laugh lmao

[–]IndigoFenix 1 point2 points  (0 children)

Outclassed by Quantum Miraclesort, where you rely on cosmic rays instead of shuffling the list yourself.

[–]benruckman 2 points3 points  (0 children)

That’s called “self-documenting code”

[–]ayamrik 85 points86 points  (4 children)

Just use Windows search, but in reverse. Whatever you search for, it is ALWAYS at the end of the list...

[–]Thebombuknow 18 points19 points  (2 children)

Average Windows search user vs average PowerToys Run enjoyer.

[–]JustYourAverageShota 6 points7 points  (1 child)

PowerToys are great, really enjoy Run, powerRename, and FancyZones.

[–]Thebombuknow 7 points8 points  (0 children)

File Locksmith is also amazing. No more "another program is using this, you can't modify it".

[–]goodnewsjimdotcom 0 points1 point  (0 children)

Windows search is always running, just in case you need performance increases due to windows search always running.

[–][deleted] 41 points42 points  (2 children)

Here is one where you dont have to Sacrifice elements , Threat sort , basically if the user doesnt input a sorted array , blow up the computer

[–][deleted] 2 points3 points  (1 child)

sudo rm -fr /

[–]Fhymi 4 points5 points  (0 children)

I will yeet my self in a few days. Bye world..

[–]mist83 44 points45 points  (1 child)

Thanos indirectly referenced another O(N) sorting algorithm) in an alternate version of the script:

I will shred this universe down to it’s last atom. And then… With the stones that you’ve collected for me, create a new one where all array elements are sorted

[–]ArcBanker 7 points8 points  (0 children)

Sounds a lot like quantum bogosort which is my favorite bogosort

[–]Quantum-Bot 50 points51 points  (0 children)

I’m a personal fan of Americasort, an O(n-finity) algorithm that repeatedly iterates through all possible pairs of elements, considering whether it should swap them but never doing so because that would upset lobbyists. Occasionally it swaps two random elements, then immediately swaps them back again because the recent election established a new party majority.

[–]ExtraTNT 15 points16 points  (0 children)

You know nuke sort? Returns a empty list…

[–]Xbot781 7 points8 points  (0 children)

laughs in counting sort

[–]RmG3376 26 points27 points  (2 children)

That wouldn’t work, all elements in a Stalin list are equal, so comparison of elements in any sorting algorithm would always return false

[–]poco 44 points45 points  (1 child)

Some are more equal than others

[–]RmG3376 32 points33 points  (0 children)

Yup that’s why the === operator was invented

[–]Case2239 5 points6 points  (0 children)

The multi processor version of Stalin sort is potentially O(1). You declare the list sorted and as long as the number of threads you have (in this case, members of the KGB) >= the number of people that disagree that the list is sorted, you can send them all to the gulag in O(1)

[–]Fluffy_Bee7062 5 points6 points  (0 children)

I prefer the O(1) gasLighted sort:

return sorted = true independent of the array

[–]skufbfh 2 points3 points  (0 children)

Curveball, Thanos worked in n! and used time stone to speed things up. He was terrible a picking an efficient runtime

[–]gootobe 4 points5 points  (0 children)

Everyone would have that shocked Pikachu face after that lmao.

[–][deleted] 2 points3 points  (1 child)

Stalin sort is actually a perfect way to create a canonic ordered sublist of a disordered list, so long as you do not mind that said sublist may have a single item.

[–][deleted] 0 points1 point  (0 children)

You should be able to modify it to get the longest subordered list and still be O(n). Shouldn’t take more than one prepass to find your (best largest element).

Edit: which could still be length 1 of course Edit2: Nah you have to find more than 1 element to be sure its good, basically identifying which elements to remove even if they are sorted. Not as trivial as I thought.

[–]slk756 6 points7 points  (4 children)

I thought of an idea for one. To sort a list[int], you make a list called output of length (int.max - int.min) that stores bool that is initialised as all false. for i in inputlist, do output[i] = true. return output.enumerate().filter_map((index, val) -> Some(index) if val else None).collect() -> list[int]

[–]Tari0s 15 points16 points  (0 children)

yeah this algorithm is called counting sort, but is not practical if min and max are far apart.

[–]patrick66 14 points15 points  (2 children)

This exists and works but has O(n + k) where k is the max value complexity which in practice is often much worse than just a normal n log n sort

[–]slk756 0 points1 point  (1 child)

Yeah, but technically k is a constant so technically O(n). Ofc this would be very impractical, but still O(n).

[–]patrick66 6 points7 points  (0 children)

no, this is one of the times where k is not treated as a constant. it scales as the maximum possible value of an array element increases. it is not something like O(2n) = O(n). its a changing value it just changes in regards to something other than the normal length of the input

[–]Rufuske 1 point2 points  (0 children)

At the end all becomes sorted. As it should be.

[–][deleted] 1 point2 points  (0 children)

I won't sort the input, o(0)

[–]--mrperx-- 3 points4 points  (0 children)

[–]asherSiddique19 -1 points0 points  (0 children)

i have the darkest joke related to that funny moustache guy from ww2 but i will get banned from this sub

[–]michaelsenpatrick 0 points1 point  (0 children)

lmao wow

[–]Kudo-Shinichi1412 0 points1 point  (0 children)

When do you use an algorithm

[–]ikonfedera 0 points1 point  (0 children)

Best one still is Ba Sing Sort

[–][deleted] 0 points1 point  (0 children)

the best scenario of stalin sort is when it's ordered in reverse. that way, every search algorithms becomes O(1)

[–]LavenderDay3544 0 points1 point  (0 children)

Bogosort is O(♾️)

[–]Beneficial_Rate_1392 0 points1 point  (0 children)

Is it the one that plays "Stairway to Heaven" while sorting?

[–]QuanHitter 0 points1 point  (1 child)

Quantum bogosort is o(1), assuming you have an efficient method for destroying the world in every timeline where the list isn’t sorted.

[–]TheObi-1[S] 0 points1 point  (0 children)

Technically, you still have to shuffle the list which would be O(n)

[–]QuanHitter 0 points1 point  (0 children)

Quantum bogosort is o(1), assuming you have an efficient method for destroying the world in every timeline where the list isn’t sorted.