Stable vs. unstable ordering

2

What defines a stable sort algorithm?

In this question I've been told a bit about stable and unstable ordering, but I still do not see the advantage of using an unstable one.

In what cases can we use unstable ordering? Is it always preferable to use a stable?

Depending on the data structure we use, you need to be careful about unstable ordering?

  

A sort algorithm is considered stable when it can preserve the order of registration of equal keys, in other words if the records appear in the ordered sequence in the same order as they are in the initial sequence.

I did not realize this explanation, what would the records be? Ex:

int vec[5]={4,2,5,1,7};

If I wanted to sort this vector what would it be to preserve the order of the records?

    
asked by anonymous 26.08.2018 / 23:01

1 answer

4

Here I go through the Occam Razor : if all solutions give the same result I choose the simplest one. Which can be the most efficient.

In general if you have 2 "josé" and nothing else that differentiates them both makes which of them comes first after classified and therefore both what algorithm to use. But if the sorted list being assembled needs to consider the order of entry in the list then the stable algorithm is mandatory. Basically the question you should ask is whether the position in the original listing is part of the tiebreaker or not, if it is important information you need to use the stable algorithm.

Any unstable classification algorithm can be made stable by modifying the sort key by manually adding the position, as long as it is available.

As always, it is a question of what guarantees you need and what commitments you accept. Some algorithms give up some efficiency to provide stability. But this is not guaranteed, depending on the comparison the stable can have more efficiency than the unstable.

It's not that you need to be careful about a particular data structure, but be careful about the desired need.

For example, a spreadsheet has no clear position, so it does not make sense to require stability if this is the source.

Remembering that there can only be instability in case of a tie. If the structure guarantees to have unique key stability is guaranteed for any algorithm. So the example quoted either does if the algorithm is stable or not, after all there is no tie in 2 elements of it. Read the linked question again because you have not yet understood what the stability is.

Particularly I prefer the stable whenever it makes no difference or it does not matter, but it's common to make a difference.

It may be that in the future you need the original order, even if you do not need it now, you would have to re-sort it in the original if it is available.

In addition, stable algorithms tend to have more predictability of resource consumption.

    
27.08.2018 / 02:56