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

  • 2
    For parallelization purposes? eg: merge sort is stable and can be parallelized well and so is quicksort. – DarthVader Oct 5 '09 at 0:46
  • 9
    Classic QuickSort is unstable – Konstantin Spirin Oct 5 '09 at 2:00
  • 2
    stable sort algo - IBM (Insertion, Bubble, Merge) – roottraveller Jul 17 '17 at 9:11
  • 1
    @roottraveller excuse me? – DarthVader Jul 17 '17 at 10:03
  • 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 not what 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 Jun 27 at 3:50
up vote 205 down vote accepted

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:


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


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.

  • 1
    Sounds good. Any other reasons ? – DarthVader Oct 5 '09 at 0:53
  • 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. – Joey Adams Jun 24 '12 at 4:40
  • 2
    I do not understand the line ..same sorting key ? What do you mean by key here ? Please explain the statement ..same sorting key – saplingPro Nov 5 '12 at 12:54
  • 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 Oct 18 '13 at 15:30
  • 7
    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 Jul 17 '17 at 9:16

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

enter image description here

  • Your values are not equal. You compare 9,7 and 9,8 but according to stability check you need the same values like both 9,7 or both 9,8. And than same values should be ordered in same in stable algorithms. – erhun May 21 '17 at 8:47
  • No, to check the stability your values should be same. I mean assume that you use two 9,7 and name it at node A and node B. If every sort operation order is like A,B (instead of they are equal) understand that sorting algorithm is stable(like merge sort). If A, B order is change when sort them multiple times(1. sort A,B then B,A again A,B etc.), understand that sorting algorithm is unstable(like quick sort) @snr – erhun May 23 '17 at 11:04
  • It compares in terms of x values. The example demonstrates x and y values. (i.e. structure). Eksik bir yaklaşımınla da olsa yine de bir yeri yanlış yapmışım onu düzeltmiş oldum sağol yinede sadece tevafuk.. – snr May 24 '17 at 14:46
  • @snr [9, 6] is not present in Input Array. I think you meant [9, 8] in last array strip. – Undefined Nov 4 '17 at 21:15

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.

  • 4
    I think you mean "last AND given name". The surname typically is the last name. – Bacon Bits Jun 2 '16 at 17:27

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.

  • Stable algorithms like Merge Sort have the same O(NlogN) complexity as Quicksort; the constant multiplier on the effort is bigger, though. – Jonathan Leffler Oct 5 '09 at 2:14
  • 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 Oct 5 '09 at 4:40
  • reaily -> readily – Bob Murphy Oct 5 '09 at 4:41
  • 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 Feb 10 '13 at 1:43

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).


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

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.

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.


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 May 22 '13 at 13:03
  • 2
    That's not what stability means. See en.wikipedia.org/wiki/Sorting_algorithm#Stability – Luís Oliveira Nov 22 '13 at 15:00
  • I 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 Nov 22 '13 at 16:38
  • 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 Oct 20 '16 at 15:13
  • 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 Oct 16 '17 at 15:46

Your Answer


