Why merge sort is stable sort




















Bob Murphy Bob Murphy 5, 2 2 gold badges 29 29 silver badges 35 35 bronze badges. Stable algorithms like Merge Sort have the same O NlogN complexity as Quicksort; the constant multiplier on the effort is bigger, though. The reason I mentioned Quicksort is that qsort is a C standard library routine, so it's reaily available. Best overall answer IMHO. The fact that stable sort produces a predictable result can be important in some apps.

In particular if you have two input lists A,B which are identical except list B has an extra entry, the outputs for a stable sort will be identical except that B has that same extra entry.

In the last sentence, I don't understand what you mean with "last-place key for each record" - could you please explain? Very good informative comment overall : — augenss. That will preserve the two keys' original order when you do the sort. Then when you're done with the sort, change both keys back to "foo". It depends on what you do. I think you mean "last AND given name". The surname typically is the last name. Clinton Pierce Clinton Pierce What does record swapping have to do with stability?

If you retain order then for certain input it may have less "churn" of elements which would have caused extra memory page writes FWIW — rogerdpack. I know there are many answers for this, but to me, this answer , by Robert Harvey , summarized it much more clearly: A stable sort is one which preserves the original order of the input set, where the [unstable] algorithm does not distinguish between two or more items.

M Ciel M Ciel 85 7 7 bronze badges. Stable sort will always return same solution permutation on same input. Stable sort algorithm is necessary deterministic. Luka Rahne Luka Rahne 9, 3 3 gold badges 31 31 silver badges 55 55 bronze badges. That's not what stability means. See en. I should correct last sentence than non stable sort can output different solution even among same implementation, where any stable sort outputs same solution. Why -1? Can someone point out please what is wrong here?

This is not what stably sort is, but what property stable sort has. Whether the sort is deterministic or not does not determine whether it is stable. I can write a non-stable deterministic sort algorithm by defining a different tie-breaking behavior by subsorting non-key parts, for example. Stable sort specifically implies that the pre-sorted relative order of the elements are preserved when ties are sorted.

I can make a deterministic sort that always deterministically outputs: [ 1,3 , 1,5 , 3,3 , 5,3 ] but this is not a stable sort. That is no matter witch stable sort algorithm or implementation is used, every time there will be same result. It is harder to maintain such property among different non stable sort implementations.

The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked See more linked questions. Merge Sort- Merge sort is a famous sorting algorithm.

It uses a divide and conquer paradigm for sorting. It divides the problem into sub problems and solves them individually. It then combines the results of sub problems to get the solution of the original problem. How Merge Sort Works? Create variable k for sorted output array. Then, we increment i and k by 1.

Then, we increment j and k by 1. Then, we have- Step Clearly, all the elements from right sub array have been added to the sorted output array. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics.

Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Do we care for simple arrays like array of integers?

When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. Stability is also not an issue if all keys are different. An example where it is useful Consider the following dataset of Student Names and their respective class sections. If we sort this data according to name only, then it is highly unlikely that the resulting dataset will be grouped according to sections as well.

So we might have to sort again to obtain list of students section wise too. But in doing so, if the sorting algorithm is not stable, we might get a result like this-. The dataset is now sorted according to sections, but not according to names.

In the name-sorted dataset, the tuple was before , but since the sorting algorithm is not stable, the relative order is lost.

If on the other hand we used a stable sorting algorithm, the result would be- Here the relative order between different tuples is maintained. It may be the case that the relative order is maintained in an Unstable Sort but that is highly unlikely.

Which sorting algorithms are stable?



0コメント

  • 1000 / 1000