Short answer
Bubble sort, insertion sort, merge sort, and Timsort are stable. Selection sort, heapsort, and Shell sort are not. Quicksort is the one to check: the usual in-place form is not stable, although stable variants exist. If stability matters for your data, confirm it for the exact sort you call.
Stability only matters when two items have the same sort key but differ in other ways. The sections below explain what stability guarantees, why it matters, and how common languages handle it.
What a stable sort guarantees
A sorting algorithm is stable if items with equal keys come out in the same relative order they went in. If two items share a key, whichever appeared first in the input still appears first in the output.
Stability only changes results when tied items can be told apart. Sorting a list of plain integers gives the same answer either way, because two 5s look identical. The difference shows up when each item carries extra data, such as a name or a row from a table.
A small example
Take these records, listed in the order they were entered:
- Ana, age 30
- Ben, age 25
- Cal, age 30
- Dee, age 25
A stable sort by age gives:
- Ben, age 25
- Dee, age 25
- Ana, age 30
- Cal, age 30
Ben still comes before Dee, and Ana still comes before Cal, just as in the input. An unstable sort may return Dee before Ben, or Cal before Ana. Both results are sorted by age, but only the stable one keeps the original order within each age.
Why stability matters
Stability is most useful when you sort in stages. Sort by the less important field first, then sort by the more important field with a stable sort. The final order follows the important field, and ties are broken by the earlier sort.
Suppose you want a class roster sorted by class, with names in alphabetical order inside each class. Sort by name first. Then sort by class with a stable sort, and the names inside each class stay alphabetical.
Stability also keeps results predictable when someone re-sorts a table. If a table is already in name order, sorting it again by department keeps the names in order within each department.
If your code depends on tied items staying in order, don’t assume it. An unstable sort can pass simple tests and still reorder tied items on other data.
Stable sorting algorithms
- Bubble sort swaps only neighbors that are out of order. Two equal items are never swapped past each other, so their order can’t change.
- Insertion sort moves each item left only while the item before it is strictly larger. An equal item stops the move, so tied items keep their order.
- Merge sort splits the list, sorts each half, and merges them. If the merge takes from the left half when the two front items are equal, the sort is stable. If you’re asking which sorting algorithms use recursion, merge sort is a clear example, since it sorts each half the same way it sorts the whole.
- Timsort is a hybrid built on merge sort, and it is stable. It is the algorithm behind Python’s built-in sort and Java’s sort for object arrays.
- Counting sort and radix sort are stable when written in the usual way. Radix sort depends on that stability to give correct results.
Unstable sorting algorithms
- Selection sort finds the smallest remaining item and swaps it into place. A long swap can carry an item past another item with an equal key. Take [2a, 2b, 1], where 2a and 2b both have the key 2. Selection sort swaps 2a with 1, giving [1, 2b, 2a]. The two 2s have traded places.
- Heapsort builds a heap and repeatedly removes the top item. Equal items can come out of the heap in any order. The heapsort algorithm is an in-place algorithm, so it needs almost no extra memory, but it is not stable.
- Quicksort picks a pivot and partitions the list around it, which can move items across long distances. Standard in-place partitioning can reverse the order of equal keys. Stable variants exist, but they need extra memory or extra logic, so the usual versions are not stable.
- Shell sort moves items by large gaps first. An item can jump past an equal item, so tied items can change order.
Stability at a glance
| Algorithm | Stable? | Worst-case time | Extra memory |
|---|---|---|---|
| Bubble sort | Yes | O(n²) | O(1) |
| Insertion sort | Yes | O(n²) | O(1) |
| Selection sort | No | O(n²) | O(1) |
| Merge sort | Yes | O(n log n) | O(n) |
| Quicksort (usual form) | No | O(n²) | O(log n) |
| Heapsort | No | O(n log n) | O(1) |
| Timsort | Yes | O(n log n) | O(n) |
Quicksort’s average time is O(n log n), and its worst case is O(n²). Counting sort is not in the table because it doesn’t compare items, but it is stable in its usual form.
Making any sort stable
You can make an unstable comparison sort stable by adding each item’s original position as a tie-breaker. When two keys are equal, the item that came first in the input compares as smaller. This costs extra memory to store the positions and a little extra work per comparison. It is a common fallback when the sort you have isn’t stable.
Stability in common languages
- Python:
sorted()andlist.sort()are documented to be stable. - Java:
Arrays.sort()on object arrays andCollections.sort()are stable.Arrays.sort()on primitive arrays such asint[]uses dual-pivot quicksort, which is not stable. That doesn’t matter in practice, because equal primitive values can’t be told apart. - C++:
std::stable_sortkeeps equal items in order.std::sortdoes not guarantee it.
Check the documentation for the exact function you call, since names don’t always reveal the answer.
Testing a sort for stability
Build a short list where several items share keys, and tag each item with its original position. Run the sort, then check whether the tied items still appear in increasing order of their original positions. If they don’t, the sort is unstable for that input. One passing test doesn’t prove stability for every input, but a failing test shows the sort is not stable.
Does stability make a sort faster?
Not necessarily. Stability describes the order of tied items, not speed. Merge sort is stable and has a worst case of O(n log n), but it needs extra memory. Heapsort is unstable, has the same worst case, and works in place. Quicksort is unstable, yet it is often fast in practice. Speed is a separate question, and the answer to what is the most efficient sorting algorithm depends on your data and limits, not on stability alone.
When you choose a sort, weigh three things: whether tie order matters, how much memory you have, and whether you need a guaranteed worst case.
Key takeaways
- A stable sort keeps items with equal keys in their original order.
- Bubble sort, insertion sort, merge sort, Timsort, counting sort, and radix sort are stable in their usual forms.
- Selection sort, heapsort, Shell sort, and the usual form of quicksort are not.
- When tie order matters, use a stable library function or add an original-position tie-breaker.