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.


Solution 1:

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.

Solution 2:

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

Solution 3:

A priority queue is an example of this. Say you have this:

  1. (1, "bob")
  2. (3, "bill")
  3. (1, "jane")

If you sort this from smallest to largest number, an unstable sort might do this.

  1. (1, "jane")
  2. (1, "bob")
  3. (3, "bill")

...but then "jane" got ahead of "bob" even though it was supposed to be the other way around.

Generally, they are useful for sorting multiple entries in multiple steps.

Solution 4:

Not all sorting is based upon the entire value. Consider a list of people. I may only want to sort them by their names, rather than all of their information. With a stable sorting algorithm, I know that if I have two people named "John Smith", then their relative order is going to be preserved.

Last     First       Phone
-----------------------------
Wilson   Peter       555-1212
Smith    John        123-4567
Smith    John        012-3456
Adams    Gabriel     533-5574

Since the two "John Smith"s are already "sorted" (they're in the order I want them), I won't want them to change positions. If I sort these items by last, then first with an unstable sorting algorithm, I could end up either with this:

Last     First       Phone
-----------------------------
Adams    Gabriel     533-5574
Smith    John        123-4567
Smith    John        012-3456
Wilson   Peter       555-1212

Which is what I want, or I could end up with this:

Last     First       Phone
-----------------------------
Adams    Gabriel     533-5574
Smith    John        012-3456
Smith    John        123-4567
Wilson   Peter       555-1212

(You see the two "John Smith"s have switched places). This is NOT what I want.

If I used a stable sorting algorithm, I would be guaranteed to get the first option, which is what I'm after.