Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit for a sort algorithm to be stable?

A sort is said to be stable if it maintains the relative order of elements with equal keys. I guess my question is really, what is the benefit of maintaining this relative order? Can someone give an example? Thanks.

like image 521
hsebastian Avatar asked Apr 30 '09 19:04

hsebastian


People also ask

Why is stability important in sorting algorithm?

A stable sorting algorithm maintains the relative order of the items with equal sort keys. An unstable sorting algorithm does not. In other words, when a collection is sorted with a stable sorting algorithm, items with the same sort keys preserve their order after the collection is sorted.

What is the benefit of sorting algorithm?

The quick sort is regarded as the best sorting algorithm. This is because of its significant advantage in terms of efficiency because it is able to deal well with a huge list of items. Because it sorts in place, no additional storage is required as well.

When can a sorting algorithm be stable?

A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Informally, stability means that equivalent elements retain their relative positions, after sorting.

Is the stability of a sorting algorithm more likely to be a concern?

When sorting an array of primitive values by their actual full values, the notion of stability is not really relevant. If you have an array [2,1,3,2] and you sort it then you get [1,2,2,3]. The result is exactly the same whether you use a stable or unstable sort.


2 Answers

It enables your sort to 'chain' through multiple conditions.

Say you have a table with first and last names in random order. If you sort by first name, and then by last name, the stable sorting algorithm will ensure people with the same last name are sorted by first name.

For example:

  • Smith, Alfred
  • Smith, Zed

Will be guaranteed to be in the correct order.

like image 96
Matt Brunell Avatar answered Oct 28 '22 11:10

Matt Brunell


A sorting algorithm is stable if it preserves the order of duplicate keys.

OK, fine, but why should this be important? Well, the question of "stability" in a sorting algorithm arises when we wish to sort the same data more than once according to different keys.

Sometimes data items have multiple keys. For example, perhaps a (unique) primary key such as a social insurance number, or a student identification number, and one or more secondary keys, such as city of residence, or lab section. And we may very well want to sort such data according to more than one of the keys. The trouble is, if we sort the same data according to one key, and then according to a second key, the second key may destroy the ordering achieved by the first sort. But this will not happen if our second sort is a stable sort.

From Stable Sorting Algorithms

like image 23
dirkgently Avatar answered Oct 28 '22 10:10

dirkgently