156

58

I'm very curious, why stability is or is not important in sorting algorithms?

156

58

I'm very curious, why stability is or is not important in sorting algorithms?

228

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. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.

*Background*: a "stable" sorting algorithm keeps the items with the same sorting key in order. Suppose we have a list of 5-letter words:

```
peach
straw
apple
spork
```

If we sort the list by just the first letter of each word then a stable-sort would produce:

```
apple
peach
straw
spork
```

In an **unstable** sort algorithm, `straw`

or `spork`

may be interchanged, but in a stable one, they stay in the same relative positions (that is, since `straw`

appears before `spork`

in the input, it also appears before `spork`

in the output).

We could sort the list of words using this algorithm: stable sorting by column 5, then 4, then 3, then 2, then 1. In the end, it will be correctly sorted. Convince yourself of that. (by the way, that algorithm is called radix sort)

Now to answer your question, suppose we have a list of first and last names. We are asked to sort "by last name, then by first". We could first sort (stable or unstable) by the first name, then stable sort by the last name. After these sorts, the list is primarily sorted by the last name. However, where last names are the same, the first names are sorted.

You can't stack unstable sorts in the same fashion.

1Sounds good. Any other reasons ? – DarthVader – 2009-10-05T00:53:51.740

So, what would the sort be called to make the words in correct sorting order of apple peach sport straw? The stable sort gave us apple peach straw spork however st should be after sp (alphabetically correct), so the ultimate correct sort should be apple peach sport straw – user1416486 – 2012-06-24T01:43:28.350

2

@user1416486: We're sorting by the first letter only. With that assumption, `straw`

and `spork`

compare equal. Stable sort will preserve the order of input, whereas unstable sort does not make that guarantee. "Correct" depends on the application. The sort function in most programming languages lets the user supply a custom ordering function. If the user's function treats different items as equal (e.g. same first name, different last name), it helps to know if the original order will be preserved. See OCaml's array sorting functions for a real-world example.

2I do not understand the line *..same sorting key* ? What do you mean by key here ? Please explain the statement *..same sorting key* – saplingPro – 2012-11-05T12:54:13.610

1@saplingPro: by "sorting key", I mean the thing you are sorting items by. So when sorting by first letter, then for each item, its "sorting key" is its first letter. – Joey Adams – 2013-10-18T15:30:40.463

@JoeyAdams I just added one paragraph to beginning of your answer . hope it is ok with you. :) – Kick Buttowski – 2015-02-20T21:52:16.477

1@KickButtowski: Thanks, looks good! – Joey Adams – 2015-02-20T22:08:02.830

@JoeyAdams Can you please add the information in your comment into your answer. I was about to down vote this as `spork`

does come before `straw`

unless you are only sorting by the first letter. To me that is not a natural way to sort strings and should be made clear. – NathanOliver – 2016-08-22T12:08:29.610

@NathanOliver: I'm not sure how to word it; mind giving it a shot? I do say "stable-sorting by the first letter", but I guess it's easy to skim over that. – Joey Adams – 2016-08-23T01:12:55.317

@JoeyAdams I gave it a tweek. I did skip right over where you said sort by the first letter. I think this might draw a little more attention to it. If you do not like it just roll it back. It is good either way, I just need to slow down on my reading. Thanks. – NathanOliver – 2016-08-23T11:38:09.153

@JoeyAdams so this applies only to strings, for numbers, all algos will be stable? – garg10may – 2016-12-23T07:48:13.580

8**Example -** Say you have a list with each item having information about destination of the flight and departure time. You first sort the list based on time. We then sort it based on destination. If the second sort is **stable** we now have all flights bound to same destination together and in increasing order of departure time. If it wasn't stable, they wouldn't be in increasing order of time. – roottraveller – 2017-07-17T09:16:09.457

@rootraveller your example helped understanding "stability" more. Thank you. :) – Undefined – 2017-11-04T23:53:11.087

25

*A stable sorting algorithm* is the one that sorts the identical elements in their same order as they appears in the input, whilst unstable sorting *may not* satisfy the case.

Stable Sorting Algorithms:

- Insertion Sort
- Merge Sort
- Bubble Sort
- Tim Sort
- Counting Sort

Unstable Sorting Algorithms:

- Heap Sort
- Selection sort
- Shell sort
- Quick Sort

15

Sorting stability means that records with the same key retain their relative order before and after the sort.

So stability matters if, and only if, the problem you're solving requires retention of that relative order.

If you don't need stability, you can use a fast, memory-sipping algorithm from a library, like heapsort or quicksort, and forget about it.

If you need stability, it's more complicated. Stable algorithms have higher big-O CPU and/or memory usage than unstable algorithms. So when you have a large data set, you have to pick between beating up the CPU or the memory. If you're constrained on both CPU and memory, you have a problem. A good compromise stable algorithm is a binary tree sort; the Wikipedia article has a pathetically easy C++ implementation based on the STL.

You can make an unstable algorithm into a stable one by adding the original record number as the last-place key for each record.

1Stable algorithms like Merge Sort have the same O(NlogN) complexity as Quicksort; the constant multiplier on the effort is bigger, though. – Jonathan Leffler – 2009-10-05T02:14:25.260

Yes, and memory usage on Merge Sort is O(N), while on Quicksort it's O(log N). The reason I mentioned Quicksort is that qsort() is a C standard library routine, so it's reaily available. – Bob Murphy – 2009-10-05T04:40:43.503

reaily -> readily – Bob Murphy – 2009-10-05T04:41:15.563

Best overall answer IMHO. the multi-key technique mentioned in others is interesting but overrated; it's simple to apply, but tends to be far slower than obvious alternatives (just use one sort with a multi-key compare; or sort by the first key then identify and sort any sublists with duplicates). 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. And +1 for last pgph. – greggo – 2013-02-10T01:43:22.183

14

It depends on what you do.

Imagine you've got some people records with a first and a last name field. First you sort the list by first name. If you then sort the list with a stable algorithm by last name, you'll have a list sorted by first name AND last name.

4I think you mean "last AND given name". The surname typically is the last name. – Bacon Bits – 2016-06-02T17:27:22.653

10

There's a few reasons why stability can be important. One is that, if two records don't need to be swapped by swapping them you can cause a memory update, a page is marked dirty, and needs to be re-written to disk (or another slow medium).

2

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 unsorted array. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc.

However, any given sorting algo which is not stable can be modified to be stable. There can be sorting algo specific ways to make it stable, but in general, any comparison based sorting algorithm which is not stable by nature can be modified to be stable by changing the key comparison operation so that the comparison of two keys considers position as a factor for objects with equal keys.

References: http://www.math.uic.edu/~leon/cs-mcs401-s08/handouts/stability.pdf http://en.wikipedia.org/wiki/Sorting_algorithm#Stability

2

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.

1

If you assume what you are sorting are just numbers and only their values identify/distinguish them (e.g. elements with same value are identicle), then the stability-issue of sorting is meaningless.

However, objects with same priority in sorting may be distinct, and sometime their relative order is meaningful information. In this case, unstable sort generates problems.

For example, you have a list of data which contains the time cost [T] of all players to clean a maze with Level [L] in a game. Suppose we need to rank the players by how fast they clean the maze. However, an additional rule applies: players who clean the maze with higher-level always have a higher rank, no matter how long the time cost is.

Of course you might try to map the paired value [T,L] to a real number [R] with some algorithm which follows the rules and then rank all players with [R] value.

However, if stable sorting is feasible, then you may simply sort the entire list by [T] (Faster players first) and then by [L]. In this case, the relative order of players (by time cost) will not be changed after you grouped them by level of maze they cleaned.

PS: of course the approach to sort twice is not the best solution to the particular problem but to explain the question of poster it should be enough.

0

Stable sort will always return same solution (permutation) on same input.

For instance [2,1,2] will be sorted using stable sort as permutation [2,1,3] (first is index 2, then index 1 then index 3 in sorted output) That mean that output is always shuffled same way. Other non stable, but still correct permutation is [2,3,1].

Quick sort is not stable sort and permutation differences among same elements depends on algorithm for picking pivot. Some implementations pick up at random and that can make quick sort yielding different permutations on same input using same algorithm.

Stable sort algorithm is necessary deterministic.

Could you elaborate? – piperchester – 2013-05-22T13:03:26.537

2

That's not what stability means. See http://en.wikipedia.org/wiki/Sorting_algorithm#Stability

– Luís Oliveira – 2013-11-22T15:00:36.487I should correct last sentence than non stable sort can output different solution even among same implementation, where any stable sort outputs same solution. – Luka Rahne – 2013-11-22T16:38:53.377

Why -1 ? Can someone point out please what is wrong here? This is not what stably sort is, but what property stable sort has. – Luka Rahne – 2016-10-20T15:13:58.433

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. example of an output of a stable sort: `sort([(5,3),(1,5),(3,3),(1,3)], x) => [(1,5),(1,3),(3,3),(5,3)]`

. 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. – cowbert – 2017-10-16T15:46:42.217

@cowbert It is more statement, about nice property that every stable sort has. 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. – Luka Rahne – 2017-10-16T18:57:03.433

2For parallelization purposes? eg: merge sort is stable and can be parallelized well and so is quicksort. – DarthVader – 2009-10-05T00:46:26.750

11Classic QuickSort is unstable – Konstantin Spirin – 2009-10-05T02:00:01.687

2stable sort algo -

`IBM (Insertion, Bubble, Merge)`

– roottraveller – 2017-07-17T09:11:06.8631@roottraveller excuse me? – DarthVader – 2017-07-17T10:03:02.140

A note for those who might misunderstood the concept like me:

The order of equal elements is guaranteed to be preserved.means: if the elements in stable sort are considered equal, then they would follow the previous order.It's notwhat I used to think: if the elements in previous order are considered equal, then in the coming stable sort, they would follow the previous order.Though you may find the latter understanding also makes sense in many cases. – Rick – 2018-06-27T03:50:41.790