Marge Sort
from fossilesque@mander.xyz to science_memes@mander.xyz on 06 Feb 19:23
https://mander.xyz/post/24599777

#science_memes

threaded - newest

Eheran@lemmy.world on 06 Feb 20:26 next collapse

How does the last step sort an of the sizes? Why even have all the other steps if that one can do it all?

SmoothLiquidation@lemmy.world on 06 Feb 20:31 next collapse

When you merge two sorted lists, you only have to compare the first element of each, since you can trust that all of the other elements are bigger. All the steps before that are there to make sure that is true.

IrateAnteater@sh.itjust.works on 06 Feb 21:00 collapse

Wait, how do I know that all four of the right half aren’t smaller than all four of the Left half?

bstix@feddit.dk on 06 Feb 21:18 next collapse

You don’t, and they can be.

Watch the animation on Wikipedia: en.wikipedia.org/wiki/Merge_sort

SmoothLiquidation@lemmy.world on 07 Feb 00:24 collapse

It doesn’t matter. You check the first of each group and pick the smallest, then compare the one you didn’t pick with the next one of the other group. In your example, you would pick all of the ones from the right side and once it is empty, just add all the ones on the left.

CaptainBlagbird@lemmy.dbzer0.com on 07 Feb 08:23 next collapse
Ravi@feddit.org on 07 Feb 08:51 next collapse

The one and only way to learn sorting algorithms: youtu.be/dENca26N6V4?si=pDZoR0uRiuGoqwkr

bitjunkie@lemmy.world on 07 Feb 17:36 collapse

I’d watch this if I didn’t know about programming just for the sheer weirdness

Iron_Lynx@lemmy.world on 07 Feb 13:14 collapse

If you want to zipper two sorted lists, you compare the first element of each list, pick that first, take the next element of that list, rinse & repeat until one list runs out and then just chuck the entire rest of the other list in the remaining space, even if that’s just one element. Since your two initial lists are already sorted, you can trust the combined list to also be sorted.

Eheran@lemmy.world on 07 Feb 20:05 collapse

So the point is that always only exactly 2 elements are compared and so you first have to split everything into groups of 2. Seems very inefficient for larger datasets, since you need to handle every single item over and over again and compare so so often. But not a sorting and comparison expert, so no idea if human sorting logic applies at all.

Iron_Lynx@lemmy.world on 07 Feb 23:01 collapse

Tbf, Merge Sort has a Big O of n log (n) in all cases, so it’s a pretty mid sorting algorithm in general, but it’s conceptually straightforward and easy to explain to newbies.

fushuan@lemm.ee on 09 Feb 08:10 collapse

There’s no better big O sorting method for generic lists. Heap sort has better averages but the big O is the same in the end.

cholesterol@lemmy.world on 07 Feb 11:11 collapse

Step 4 seems to do nothing?

Fiery@lemmy.dbzer0.com on 07 Feb 11:16 collapse

Step 4 splits the pair above into single elements, from step 5 on the groups are getting merged.

cholesterol@lemmy.world on 07 Feb 15:41 next collapse

Oh right, okay.

Lemmine@feddit.org on 07 Feb 18:50 collapse

*Marged

Fiery@lemmy.dbzer0.com on 07 Feb 19:28 collapse

My bad