Algorithm Visualiser by Tsvetozar Biserov

Guides

Stable sorting explained

A sort is stable when records with equal keys leave in the same relative order they arrived. It sounds like a footnote, yet it decides whether sorting by two columns works. This guide defines stability, breaks it with a three-record example, sorts the 23 algorithms here by stability, and shows a repair.

What stable means

Take records that carry a key plus other data, for example a name and a grade. Several records may share a grade. A stable sort promises that among records with the same grade, the one that came first in the input still comes first in the output. An unstable sort promises only that the grades end up in order; ties may be shuffled in any way.

Stability says nothing about speed, and it never changes where records with different keys end up. It is also irrelevant when keys are unique, or when equal elements cannot be told apart, as with plain numbers.

Why it matters: sorting by two columns

Suppose a table lists orders with a city and a date. To see orders grouped by city and, inside each city, oldest first, sort by date first and then by city. That works only if the second sort is stable, because it must leave the date order untouched within each city. With an unstable sort, the second pass scrambles what the first one arranged.

Clicking column headers in a table relies on exactly this. So does radix sort, which sorts by the least significant digit first and then by each higher digit, and which is correct only because every pass is stable.

A concrete case is a support queue sorted by priority, where tickets of equal priority should be handled in arrival order. A stable sort by priority gives first come, first served inside each level with no extra code. An unstable one quietly lets a ticket from this morning jump ahead of one from last week.

A tiny example that breaks

Three records, written as name and grade: Ben 2, Cy 2, Ava 1. Sort by grade with selection sort. The first pass finds the smallest grade, Ava at position 2, and swaps it with position 0. The result is Ava 1, Cy 2, Ben 2. Ben and Cy traded places although their grades are equal, while a stable sort would give Ava, Ben, Cy.

Insertion sort on the same input behaves differently. Cy is not smaller than Ben, so it stays. Ava is smaller than both, so each of them shifts one place right and Ava drops in front, leaving Ben before Cy. The mechanism is general: a swap across a distance can carry an element over an equal one, and a step-by-step shift cannot.

The stable algorithms on this site

Nine of the 23 are stable as written here: bubble, cocktail shaker, gnome, odd-even, insertion, merge, tim, bucket and radix sort. Bubble, cocktail shaker, gnome, odd-even and insertion sort only move an element past strictly larger ones, so equals never cross. Merge sort takes from the left half when two fronts tie. Tim sort is built from those two ideas, and radix sort needs stable passes to work at all.

Two notes on this site's versions. Counting sort here tallies values and writes them back, so it cannot carry records and is neither stable nor unstable; the textbook version that walks the input backwards using prefix sums is stable, and it is the pass radix sort uses here. Bucket sort is stable here because elements enter buckets in input order and each bucket is insertion-sorted with a strict comparison.

The unstable ones, and why

Thirteen are not: selection, cycle, comb, Shell, heap, quick, dual-pivot quick, three-way quick, intro, stooge, pancake, bitonic and bogo sort. Most share one cause, a jump between distant positions in a single move. Selection, comb, Shell and the quick sorts do it directly. Heap sort moves the root to the end, pancake sort flips whole prefixes and so reverses equal items inside them, and bitonic sort compares and exchanges across large distances. Bogo sort is random, and intro sort inherits from quick and heap sort.

The visualiser cannot show any of this. It sorts a shuffled list of the numbers 1 to n, where every value is distinct, so stability never changes what you see on screen.

Making an unstable sort stable

Attach each record's original position to it and use that as a tiebreaker: compare keys first, and if they are equal compare positions. Now no two records compare equal, so any correct sort, stable or not, must produce the stable order. The price is n extra integers and some extra comparisons. With integer keys you can pack both into one number, key times n plus position, as long as it fits.

The alternative is choosing a stable algorithm from the start, such as merge sort, which costs a buffer. Libraries make the choice explicit: C++ offers std::sort and std::stable_sort, and Python's sort is always stable. When you must sort by several fields, decide whether to compare all fields in one pass or to chain stable passes, and never chain unstable ones.

Guides