Definition · Stable sortA sorting algorithm is stable if, whenever two input records have equal keys, the one that appeared earlier in the input also appears earlier in the output. The property is about the algorithm, for every input, not about one run of it.
Equal keys are the only records the definition talks about. A sort that gets the order of unequal keys wrong is not unstable; it is broken. So stability can only be observed on a list with repeats, and it is observed by carrying something the key does not determine — here the input position, called a tag.
Example · Selection sort caught on eight keysTag the list 7 3 7 1 9 3 5 1 by position, so the records are 7#1 3#2 7#3 1#4 9#5 3#6 5#7 1#8. Insertion sort returns 1#4 1#8 3#2 3#6 5#7 7#1 7#3 9#5 and merge sort returns the same. Selection sort returns 1#4 1#8 3#6 3#2 5#7 7#3 7#1 9#5: the threes and the sevens have each come out reversed. Two pairs out of input order, and the question is settled.
That asymmetry is worth being explicit about. One reordered pair refutes stability, because stability is a claim about every input and a counterexample is enough to kill one. Zero reordered pairs on one list establishes nothing at all: there are 8! = 40 320 lists of that length and this was one. Stability is proved from the algorithm — insertion sort never moves an element past an equal one, and merge sort's merge takes from the left run when the heads are equal — never from a measurement.
Definition · In place, and adaptiveAn algorithm sorts in place if it uses Θ(1) memory beyond the array it is given. It is adaptive if its cost falls as its input gets closer to sorted, measured in whatever way the analysis names: number of inversions, or number of already ordered runs.
These two cut across stability and across the complexity class. Merge sort is stable, not in place, not adaptive in its standard form. Heapsort is in place, not stable, not adaptive. Insertion sort is stable, in place and adaptive, and quadratic. Nothing in Θ(n log n) implies any of the three, which is why the promise a situation needs has to be named before an algorithm is chosen.
The fourth promise is about what the algorithm is allowed to look at
A comparison sort may only ask, of two keys, whether one is at most the other. Every sort above is one. Discrete Mathematics' “Searching and Sorting” proves that no such algorithm can sort every array of n keys in fewer than ⌈log₂ n!⌉ comparisons, and that bound is cited throughout this course and reproved nowhere in it. A sort that reads the key itself — its digits, its value as an array index — is outside the bound rather than better than it, which is what “Counting Sort” is about.
Theorem · Two stable passes sort on two keysLet a list be sorted by a stable pass on the minor key and then by a stable pass on the major key. The result is in increasing order of major key, and within each run of equal major keys, in increasing order of minor key.
The second pass orders the major keys, which gives the first claim.
Take two records with equal major keys. The second pass is stable, so it leaves them in the order it found them, which is the order the first pass left them in. The first pass ordered the whole list by minor key, so of those two the smaller minor key came first. Both claims hold, and the only property of the second pass that was used is its stability.
□
Read that proof backwards and the misconception dissolves. If the major pass runs first, the minor pass afterwards is the one ordering the whole list, and what survives inside its ties is the major order — so the output is sorted by minor key, with major keys as a tie-break. That is a correct sort of a different thing. The lab runs both orders on the same eight records and prints the two sequences.
This composition is not a curiosity: it is the whole correctness argument of radix sort, which is d stable passes, one per digit, least significant first. “Radix Sort” makes each pass unstable on demand and lets the composition fall apart in front of you.