Short answer
A sorting algorithm is stable if items with equal keys come out in the same relative order they went in. Insertion sort, merge sort, bubble sort and Timsort are stable. Selection sort, heapsort and the usual versions of quicksort are not.
What stable means
Suppose you have three people with scores, listed in this order: Ann 90, Ben 85, Cara 90. Sorting by score gives Ben 85 first. Ann and Cara both have 90, and a stable sort keeps Ann before Cara, because she came first in the input. An unstable sort might produce Ben 85, Cara 90, Ann 90. Both results are sorted by score, but only the first keeps the original order among the equal scores.
Why stability matters
Stability is useful when you sort by more than one key. Suppose you have a list of people sorted by first name and you want it sorted by last name, with first names still in order within each last name. Sort by first name, then run a stable sort by last name. Equal last names keep the first-name order from the earlier sort. An unstable sort in the second step would break that order.
Stability also means that re-sorting a table by one column does not shuffle rows that look the same in that column.
Stable algorithms and why
- Insertion sort moves each item left only past items that are strictly larger, so it never passes an equal item.
- Merge sort merges two sorted halves. When the front items are equal, the merge takes the item from the left half first, which keeps the original order.
- Bubble sort swaps two neighbors only when the left one is strictly larger, so equal neighbors stay put.
- Timsort is built from merges and insertion sort, so it keeps equal items in their original order.
Unstable algorithms and why
- Selection sort can jump an item over others with long-distance swaps. Take the list 2a, 2b, 1, where 2a and 2b are equal. The smallest item is 1, at the end. Swapping it with the first position gives 1, 2b, 2a. The two 2s are now reversed.
- Heapsort removes items from a heap in order of their keys. Equal keys can come out in any order, so the original order is not kept.
- Quicksort partitions around a pivot by swapping items across it. Those swaps can move equal items past one another. Stable versions of quicksort exist, but they need extra work or memory.
Making an unstable sort stable
Any sort can be made stable by adding the item’s original position as a tiebreaker. When two keys are equal, the sort compares the original positions. This works, but it costs extra memory to store the positions and extra comparisons. Often it is simpler to choose a sort that is stable from the start.
Checking your language’s sort
- Python’s
sorted()andlist.sort()are documented to be stable. - Java’s
Arrays.sort()andCollections.sort()are documented to be stable for objects. For primitive arrays, stability does not matter, because equal numbers cannot be told apart. - In C++,
std::sortis not guaranteed to be stable, whilestd::stable_sortis.
If you need stability, read the documentation for the sort you are calling, since the guarantee varies between languages and libraries. For the wider question of which sort is fastest, see what is the most efficient sorting algorithm.