Stability is mainly important when we have key value pairs with duplicate keys possible (like people names as keys and their details as values). And we wish to sort these objects by keys.

**What is it?**

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.

Formally stability may be defined as,

Let

A sorting algorithm is stable if-

where

Informally, stability means that equivalent elements retain their relative positions, after sorting.

**Do we care for simple arrays like array of integers?**

When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. Stability is also not an issue if all keys are different.

**An example where it is useful**

Consider the following dataset of Student Names and their respective class sections.

If we sort this data according to name only, then it is highly unlikely that the resulting dataset will be grouped according to sections as well.

So we might have to sort again to obtain list of students section wise too. But in doing so, if the sorting algorithm is not stable, we might get a result like this-

The dataset is now sorted according to sections, but not according to names.

In the name-sorted dataset, the tuple

If on the other hand we used a stable sorting algorithm, the result would be-

Here the relative order between different tuples is maintained. It may be the case that the relative order is maintained in an Unstable Sort but that is highly unlikely.

**Which sorting algorithms are stable?**

Some Sorting Algorithms are stable by nature, such as Bubble Sort, Insertion Sort, Merge Sort, Count Sort etc.

Comparison based stable sorts such as Merge Sort and Insertion Sort, maintain stability by ensuring that-

Element

Since

Other non-comparison based sorts such as Counting Sort maintain stability by ensuring that the Sorted Array is filled in a reverse order so that elements with equivalent keys have the same relative position.

Some sorts such as Radix Sort depend on another sort, with the only requirement that the other sort should be stable.

**Which sorting algorithms are unstable?**

Quick Sort, Heap Sort etc., can be made stable by also taking the position of the elements into consideration. This change may be done in a way which does not compromise a lot on the performance and takes some extra space, possibly

**Can we make any sorting algorithm stable?**

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

This article is contributed by **Chirag Manwani**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Know Your Sorting Algorithm | Set 1 (Sorting Weapons used by Programming Languages)
- Know Your Sorting Algorithm | Set 2 (Introsort- C++’s Sorting Weapon)
- Sorting objects using In-Place sorting algorithm
- Lower bound for comparison based sorting algorithms
- Asymptotic Analysis and comparison of sorting algorithms
- Time Complexities of all Sorting Algorithms
- Loop Invariant Condition with Examples of Sorting Algorithms
- Sorting Algorithms Visualization : Bubble Sort
- Sorting Algorithms Visualization | Selection Sort
- Difference between Searching and Sorting Algorithms
- The Slowest Sorting Algorithms
- Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted
- Which sorting algorithm makes minimum number of memory writes?
- A Pancake Sorting Problem
- External Sorting
- Cartesian Tree Sorting
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sleep Sort – The King of Laziness / Sorting while Sleeping
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)