Counting

A worst case ten positions wide

Sorted input costs first-element quicksort 2,096,128 comparisons on 2,048 elements, 82 times its average. Reshuffle about eleven of the 2,048 positions and the cost halves — and it takes about ten at 128 elements, and between ten and thirteen at every size between. Reversed input costs insertion sort twice its average, and reshuffling half the positions still leaves 71% of the work. A worst case is a place in the space of inputs, and the two famous ones are places of very different sizes.

The worst case found by climbing ran a search that swaps two positions at a time towards a more expensive input, and it could not find first-element quicksort’s worst case at sixty-four elements although that worst case is sorted input, the best-known bad input in sorting. The search stopped on an ordering from which no swap helped, at half the worst count. That suggested something about sorted input itself — that it sits on a narrow peak, and that orderings a few swaps from it are far cheaper than it is.

A worst case is always reported as one input and one number. The number says how bad the worst input is. It says nothing about how many inputs are that bad, or about what the inputs near it cost, and those are what decide whether a worst case is a hazard in practice. Real data is rarely a random permutation and rarely an exact worst case either. It is sorted by something and then appended to, reversed except for a few late arrivals, or the output of a previous sort with a handful of updates. If a worst case survives a little disorder, data like that will find it. If a little disorder destroys it, data like that is safe, and the worst case is a warning about a single, exact, unusual input.

This page measures that property directly, first exactly on eight elements and then at the sizes enumeration cannot reach.

How many inputs are the worst

The simplest measure of how wide a worst case is is how many inputs share it. At eight elements every ordering can be counted.

Of the 40,320 orderings of eight elements, 128, 10,240, 432, 128, 5,760 cost insertion sort, merge sort, heapsort, first-element quicksort, median-of-three quicksort their worstHow many of the 40,320 orderings of eight elements reach each sort's worst comparison count, on a logarithmic axis, and how many of the 28 orderings one swap from the worst ordering do. Insertion sort, worst 28: 128 orderings (0.32%), and 7 of the 28 neighbours. Merge sort, worst 17: 10,240 orderings (25.40%), and 13 of the 28 neighbours. Heapsort, worst 29: 432 orderings (1.07%), and 9 of the 28 neighbours. First-element quicksort, worst 28: 128 orderings (0.32%), and 7 of the 28 neighbours. Median-of-three quicksort, worst 29: 5,760 orderings (14.29%), and 13 of the 28 neighbours.1101001,00010,000orderings that cost the worst case, of 40,320Insertion sort128 · 7 of 28 nearMerge sort10,240 · 13 of 28 nearHeapsort432 · 9 of 28 nearFirst-element quicksort128 · 7 of 28 nearMedian-of-three quicksort5,760 · 13 of 28 near40,320 orders of 8, enumeratednear: one swap from the worst ordering
Fig. 1 How many of the 40,320 orderings of eight elements reach each sort’s worst comparison count, on a logarithmic axis, and how many of the 28 orderings one swap from the worst ordering do. Insertion sort, worst 28: 128 orderings, and 7 of 28 neighbours. Merge sort, worst 17: 10,240 orderings, a quarter of all of them, and 13 of 28. Heapsort, worst 29: 432, and 9 of 28. First-element quicksort, worst 28: 128, and 7 of 28. Median-of-three quicksort, worst 29: 5,760, and 13 of 28.

The spread is a factor of eighty, and it follows the structure of the algorithms rather than their names.

First-element quicksort’s 128 is 272^7. Its count reaches 28 only when every pivot is the smallest or the largest element of the range it partitions, so that every partition peels off one element. Seven partitions of ranges longer than one, two choices each. Insertion sort’s 128 has the same form for a different reason. Inserting the element at position ii costs at most ii comparisons, and it costs exactly ii when that element is the smallest or the second smallest of those before it — two choices at each of seven insertions. Sorted input for the one and reversed input for the other are single members of a set of 128, and the other 127 are orderings no benchmark generator would name.

Merge sort’s worst case is a quarter of all inputs. Its count reaches 17 whenever every merge runs to its last element, and a merge runs to its last element exactly when the two largest elements it holds come from different halves. For the top merge of eight that happens with probability 4/7, and for each of the two merges of four below it with probability 2/3; the orderings inside each half are independent of which elements the half holds, so the three multiply, and 472323=1663\tfrac{4}{7}\cdot\tfrac{2}{3}\cdot\tfrac{2}{3} = \tfrac{16}{63} of 40,320 is exactly 10,240. A count over every input found merge sort’s worst case to be an ordering no benchmark produced; the reason no benchmark needs to produce it is on this plate, because a quarter of random orderings reach it and its margin over the mean, 17 against 15.73, is barely more than a comparison.

Median-of-three quicksort’s 5,760 is exactly one in seven. Heapsort’s 432 is about one in a hundred, and the only width among the five with no short explanation.

One swap away, and seven

Size is half the picture. The other half is the neighbourhood: how quickly the count falls as an ordering moves away from the worst. At eight elements the distance can be measured exactly, as the fewest swaps that turn one ordering into another, and every ordering sits at a distance between zero and seven from each sort’s worst.

At eight elements, one swap from the worst ordering keeps 69% of heapsort's excess over its mean and 52% of first-element quicksort'sEvery one of the 40,320 orderings of eight elements, grouped by the fewest swaps that turn it into the sort's worst ordering, and the mean comparison count of each group drawn as a share of the distance from the sort's mean over every ordering (0) to its worst case (1). Insertion sort, worst 28, mean 19.28: 28.00 at 0, 24.75 at 1, 22.50 at 2, 20.98 at 3, 19.96 at 4, 19.29 at 5, 18.85 at 6, 18.58 at 7. Merge sort, worst 17, mean 15.73: 17.00 at 0, 16.25 at 1, 15.92 at 2, 15.78 at 3, 15.74 at 4, 15.72 at 5, 15.73 at 6, 15.73 at 7. Heapsort, worst 29, mean 25.81: 29.00 at 0, 28.00 at 1, 27.26 at 2, 26.68 at 3, 26.21 at 4, 25.85 at 5, 25.57 at 6, 25.38 at 7. First-element quicksort, worst 28, mean 16.92: 28.00 at 0, 22.64 at 1, 20.18 at 2, 18.68 at 3, 17.66 at 4, 16.95 at 5, 16.46 at 6, 16.12 at 7. Median-of-three quicksort, worst 29, mean 26.30: 29.00 at 0, 27.50 at 1, 26.95 at 2, 26.66 at 3, 26.46 at 4, 26.31 at 5, 26.20 at 6, 26.13 at 7.00.510246swaps away from the worst orderingmean count, 1 = the worst case, 0 = the mean over every orderingInsertion sortMerge sortHeapsortFirst-element quicksortMedian-of-three quicksort40,320 orders of 8, enumeratedswap distance: fewest transpositions
Fig. 2 All 40,320 orderings of eight elements, grouped by how many swaps they are from each sort’s worst ordering, with each group’s mean comparison count drawn as a share of the gap between the sort’s mean over every ordering (0) and its worst case (1). One swap away, the mean count is 24.75 for insertion sort, 16.25 for merge sort, 28.00 for heapsort, 22.64 for first-element quicksort and 27.50 for median-of-three quicksort; seven swaps away it is 18.58, 15.73, 25.38, 16.12 and 26.13, every one at or below its sort’s overall mean.

Taking the gap between the mean and the worst as the thing being lost, one swap costs heapsort 31% of it, insertion sort 37%, first-element quicksort 48%, median-of-three quicksort 56% and merge sort 59%. In comparisons rather than shares, first-element quicksort’s fall is by far the largest — 5.36 comparisons in a single swap, against 3.25 for insertion sort and under two for the other three — because its gap is the largest to begin with.

The farthest orderings, seven swaps from the worst, cost no more than the sort’s average and usually less. Being as far as possible from one ordering is itself a constraint, and for all five sorts it pushes the count to the cheap side.

Eight elements are too few to separate a narrow peak from a broad one convincingly, because every ordering is within seven swaps of every other. What the plate establishes exactly is that no worst case at this size is isolated — every sort keeps between 41% and 69% of its excess one swap away — and that the size of a single swap’s effect varies by a factor of two between sorts. Whether a peak is narrow is a question about larger inputs.

Sorted input’s two thousand neighbours

At sixty-four elements the neighbourhood of sorted input is still small enough to count in full: 2,016 single swaps, each run through first-element quicksort.

Every one of the 2,016 swaps of the sorted order: 0 raise first-element quicksort's count above 2,016First-element quicksort on 64 elements, run on every order one swap away from the sorted order, which itself costs 2,016 comparisons. The neighbours cost between 714 and 2,016, mean 1527.6; 0 of the 2,016 cost more than the order they came from and 63 cost exactly the same. The cheapest exchange positions 0 and 41, or 0 and 42; the 63 swaps that involve the first position average 1047.1, and 63 of the 63 that change nothing involve the last.0501001,0001,5002,000comparisons after one swapswaps of the sorted order, out of 2,016the order itself: 2,016mean 152864 elements · every swap of two positions0 rises
Fig. 3 First-element quicksort on 64 elements, run on every order one swap away from sorted input, which costs 2,016 comparisons. The neighbours cost between 714 and 2,016, mean 1,527.6. None costs more than sorted input and 63 cost exactly the same. The cheapest two exchange the first element with the one at position 41, or at position 42.

One swap, chosen well, removes 65% of the worst case; the average swap removes 24%. The position of the swap matters more than anything else about it. The 63 swaps that involve the first position average 1,047.1 comparisons, about half the worst case, because they change the very first partition, which is the one that runs over the whole array. Swaps confined to the end of the array are met only when the ranges are already short, and every one of the 63 swaps that change nothing at all involves the last position.

So sorted input is a peak whose flanks fall steeply where the front of the array is disturbed and hardly at all where the back is. That does not mean only the front matters. On sorted input the recursion’s ranges are the suffixes of the array — the first partition takes everything, the next everything but the first element, and so on — so every position is the front of exactly one range, and a disturbance anywhere lands at the front of some range. What position decides is how long that range still is when the disturbance is met.

Disorder at two thousand elements

Above sixty-four the neighbourhood cannot be listed and has to be sampled, and the natural way to sample it is to apply a stated amount of disorder. The model here is the one used in the smoothed analysis of quicksort by Banderier, Beier and Mehlhorn. Each position of the input is marked independently with probability pp, and the values at the marked positions are shuffled among themselves. At p=0p = 0 the input is the worst case untouched; at p=1p = 1 it is a uniformly random ordering; and in between, pp is the fraction of the input that has been disturbed.

Each sort is given the most expensive input known for it: sorted input for first-element quicksort and reversed input for insertion sort, both worst cases by argument; for merge sort the input McIlroy’s adversary settles on, which costs exactly the worst case’s formula; and for median-of-three quicksort the adversary’s input too, which is quadratic but not known to be the worst.

Reshuffle 1% of sorted input and first-element quicksort falls from 82 times its average to 26.5; insertion sort on reversed input barely movesEach sort's most expensive known input on 2,048 elements, with each position marked with probability p and the marked values shuffled among themselves; 12 shuffles at each p. The vertical axis is the mean comparison count divided by the same sort's mean on random input. First-element quicksort on sorted input: 2,096,128 unshuffled, 81.87 times its random mean of 25,602; 70.36 at p = 0.001, 63.29 at p = 0.002, 46.08 at p = 0.005, 26.52 at p = 0.01, 17.90 at p = 0.02, 8.82 at p = 0.05, 4.83 at p = 0.1, 2.79 at p = 0.2, 1.42 at p = 0.5, 0.99 at p = 1. Median-of-three quicksort on the adversary's input: 1,051,648 unshuffled, 41.43 times its random mean of 25,386; 28.04 at p = 0.001, 27.47 at p = 0.002, 13.76 at p = 0.005, 6.75 at p = 0.01, 4.94 at p = 0.02, 2.81 at p = 0.05, 1.66 at p = 0.1, 1.39 at p = 0.2, 1.08 at p = 0.5, 0.99 at p = 1. Insertion sort on reversed input: 2,096,128 unshuffled, 2.00 times its random mean of 1,046,655; 2.00 at p = 0.001, 2.00 at p = 0.002, 2.00 at p = 0.005, 1.99 at p = 0.01, 1.98 at p = 0.02, 1.94 at p = 0.05, 1.88 at p = 0.1, 1.75 at p = 0.2, 1.42 at p = 0.5, 1.01 at p = 1. Merge sort on its worst input: 20,481 unshuffled, 1.03 times its random mean of 19,944; 1.03 at p = 0.001, 1.03 at p = 0.002, 1.03 at p = 0.005, 1.02 at p = 0.01, 1.02 at p = 0.02, 1.02 at p = 0.05, 1.01 at p = 0.1, 1.00 at p = 0.2, 1.00 at p = 0.5, 1.00 at p = 1.0.0010.010.11125102050fraction of positions reshuffled, pmean comparisons, in multiples of the mean on random inputFirst-element quicksort, 81.9×Median-of-three quicksort, 41.4×Insertion sort, 2.0×Merge sort, 1.0××: unshuffled2,048 elements · 12 shuffles a point1 = the mean on random input
Fig. 4 Each sort’s most expensive known input on 2,048 elements, with a fraction p of positions reshuffled, twelve shuffles at each p, drawn as the mean comparison count in multiples of the sort’s mean on random input. First-element quicksort on sorted input starts at 81.9 times its average and is at 26.5 at p = 0.01, 4.83 at p = 0.1 and 1.42 at p = 0.5. Median-of-three quicksort on the adversary’s input starts at 41.4 and is at 6.75, 1.66 and 1.08. Insertion sort on reversed input starts at 2.00 and is at 1.99, 1.88 and 1.42. Merge sort on its worst input starts at 1.03 and is at 1.02, 1.01 and 1.00.

Four lines, four different kinds of worst case.

Merge sort’s is flat on the floor. A worst case three per cent above the average is not a hazard at any level of disorder, and the plate would not distinguish it from its average without the numbers.

Insertion sort’s barely moves. Reshuffling one per cent of a reversed array leaves insertion sort doing 1.99 times its average; reshuffling ten per cent leaves 1.88. Half the array reshuffled still costs 1.42 times the average. This is a worst case that data does not have to find exactly. Any input that is mostly in reverse order is most of the way to it.

The two quicksorts’ fall off a cliff, and the cliff starts almost at once. At p=0.001p = 0.001 — on average two of the 2,048 positions reshuffled — first-element quicksort has already lost fourteen per cent of its excess. At p=0.01p = 0.01 it is down from 82 times the average to 26.5; at p=0.05p = 0.05, to under nine. The adversary’s input for median-of-three quicksort is more fragile still: twenty reshuffled positions take it from 41 times the average to under seven.

That last point deserves a sentence of its own, because it says what the adversary’s inputs are. They are built comparison by comparison to exploit a pivot rule exactly, and the construction leaves nothing to spare. The adversary who knows the seed argued that a published function can be attacked; this plate adds that the attack has to be delivered precisely, because a one per cent perturbation of the attack’s input removes 86% of its effect.

The mean hides a lottery

A mean over twelve shuffles is a summary, and on average is not a number is this collection’s standing objection to letting one stand alone. At small pp the summary is hiding something specific: whether the few reshuffled positions happened to land where they matter.

First-element quicksort on sorted input of 1,024: 81 of 120 runs keep 90% of the worst case at p = 0.002, 0 at p = 0.05Every one of 120 runs of first-element quicksort on sorted input of 1,024 elements with a fraction p of positions reshuffled, one dot each, on a logarithmic axis between the mean on random input, 11,278, and the unshuffled count, 523,776. At p = 0.002: least 173,981, mean 459,294, greatest 523,776; 81 of 120 runs at 90% of the worst case or more. At p = 0.01: least 100,176, mean 253,012, greatest 523,776; 2 of 120 runs at 90% of the worst case or more. At p = 0.05: least 35,306, mean 81,075, greatest 193,612; 0 of 120 runs at 90% of the worst case or more.10,00020,00050,000100,000200,000500,000comparisons, one dot per rununshuffled 523,776random input 11,278p = 0.002mean 459,294 · 81 of 120 at 90% of the worst or morep = 0.01mean 253,012 · 2 of 120 at 90% of the worst or morep = 0.05mean 81,075 · 0 of 120 at 90% of the worst or more1,024 elements · 120 runs a rowtick: the mean
Fig. 5 Every one of 120 runs of first-element quicksort on sorted input of 1,024 elements with a fraction p of positions reshuffled, one dot each, on a logarithmic axis between the mean on random input, 11,278, and the unshuffled count, 523,776. At p = 0.002: least 173,981, mean 459,294, greatest 523,776, and 81 of the 120 runs keep at least 90% of the worst case. At p = 0.01: least 100,176, mean 253,012, greatest 523,776, and two runs keep 90%. At p = 0.05: least 35,306, mean 81,075, greatest 193,612, and none does.

At p=0.002p = 0.002 — about two positions of 1,024 — two runs in three keep nearly the whole worst case, and the cheapest loses two-thirds of it. The distribution is not a hump around a centre but a pile against the ceiling with a long tail below it, and the mean sits below the pile, pulled down by the tail. That is what a narrow peak looks like when it is sampled: most small disturbances miss the places that matter, and the ones that hit them hit hard.

By p=0.01p = 0.01 — about ten positions — the pile has left the ceiling. Two runs in 120 keep 90% of the worst case and one of them keeps all of it; the middle of the cloud is near half of it, and the range spans a factor of five. By p=0.05p = 0.05 nothing is near the worst case, and the cheapest runs are within a factor of about three of random input.

Ten positions, at every size

The plates put the halving of first-element quicksort’s worst case near p=0.01p = 0.01 at 1,024 elements and a little above p=0.005p = 0.005 at 2,048, and that ratio is suggestive. The measurement below finds the halving point by bisection at five sizes.

The share of sorted input that must be reshuffled to halve first-element quicksort's worst case: 8.0% at 128, 0.56% at 2,048For each size, the fraction p of positions of sorted input that must be marked and reshuffled before first-element quicksort's mean over 12 shuffles falls to half the unshuffled count, found by bisection on log p. 128 elements: p = 0.0798, about 10.2 positions of 128. 256 elements: p = 0.0401, about 10.3 positions of 256. 512 elements: p = 0.0240, about 12.3 positions of 512. 1,024 elements: p = 0.0108, about 11.1 positions of 1,024. 2,048 elements: p = 0.0056, about 11.5 positions of 2,048.1282565121,0242,0480.0050.010.020.05elements sortedfraction of positions reshuffled to halve the count10.2 positions10.3 positions12.3 positions11.1 positions11.5 positions12 shuffles a bisection stepsorted input, first-element pivot
Fig. 6 For each size, the fraction of positions of sorted input that must be reshuffled before first-element quicksort’s mean count over twelve shuffles falls to half the unshuffled count, found by bisection. 128 elements: 8.0% of positions, about 10.2 of them. 256: 4.0%, about 10.3. 512: 2.4%, about 12.3. 1,024: 1.08%, about 11.1. 2,048: 0.56%, about 11.5.

The fraction falls by a factor of fourteen across a sixteen-fold range of sizes. The number of positions barely moves: between ten and thirteen at every size, with no trend the twelve-shuffle noise could not account for. A constant number of disturbances halves the worst case, not a constant fraction — and so, measured as a share of the input, first-element quicksort’s worst case gets narrower in exact proportion as the input grows.

The mechanism is visible in the plates above even though it is not proved here. The quadratic cost of sorted input comes from ranges whose first element is extreme, and a reshuffled value that reaches the front of a large range splits it at a random point instead. What matters is how many such splits happen early, while the ranges are still long, and that is a matter of how many values were reshuffled — not of what fraction of the array they were. A range of two thousand sorted elements and a range of two hundred are equally undone by a handful of splits.

The smoothed analysis that introduced this model bounds quicksort’s expected count by a constant times (n/p)logn(n/p)\log n, and it is worth being exact about what that bound does and does not say here. At ten positions, pp is 10/n10/n and the bound becomes a constant times n2lognn^2 \log n, which grows faster than the quadratic count it is bounding; in this regime the bound says nothing at all. It becomes informative when the number of reshuffled positions is large compared with logn\log n, which is where the plate’s lines have already flattened towards the average. So the constancy measured here is a fact about sizes 128 to 2,048 and twelve shuffles a point. Whether it holds at a million elements, or rises slowly with nn in a way this range is too short to show, is not settled by anything on this page.

Insertion sort’s worst case is a region

Insertion sort’s line on the first plate was nearly flat, and unlike quicksort’s it can be predicted exactly. Its comparison count is the number of inverted pairs in its input plus at most n1n - 1, so the question is how many pairs of a reversed array remain inverted after a fraction pp of positions is reshuffled.

Two unmarked positions keep their values and stay inverted. Two marked positions receive two values drawn from the shuffle and are inverted half the time. A pair with one marked and one unmarked position is the case that needs care: the marked position gets a value drawn uniformly, and the unmarked one keeps a value set by where it sits. An unmarked position near the front of a reversed array holds a large value, so a random value placed after it is usually smaller and the pair stays inverted; one near the back holds a small value and the pair usually becomes ordered. Averaged over all pairs, the first kind outweighs the second, and a mixed pair stays inverted two times in three. Adding the three cases gives the share of pairs still inverted as (1p)2+43p(1p)+12p2=123p+16p2(1-p)^2 + \tfrac{4}{3}p(1-p) + \tfrac{1}{2}p^2 = 1 - \tfrac{2}{3}p + \tfrac{1}{6}p^2.

Insertion sort on reversed input: half the positions reshuffled still leaves 71% of the work, as the pair count saysInsertion sort on 2,048 reversed elements with a fraction p of positions reshuffled, 12 shuffles at each p, as a share of the unshuffled 2,096,128 comparisons: 0.9997 at p = 0.001, 0.9993 at p = 0.002, 0.9981 at p = 0.005, 0.9954 at p = 0.01, 0.9883 at p = 0.02, 0.9688 at p = 0.05, 0.9376 at p = 0.1, 0.8750 at p = 0.2, 0.7094 at p = 0.5, 0.5030 at p = 1. The solid curve is 1 − 2p/3 + p²/6, the share of pairs still inverted when a marked and an unmarked position are inverted two times in three; at p = 1/2 it predicts 0.7083. The dashed curve treats a mixed pair as a coin flip, 1 − p + p²/2, and predicts 0.6250.0.60.810.0010.010.11fraction of positions reshuffled, pcomparisons, as a share of the unshuffled countpair count: 1 − 2p/3 + p²/6coin flip: 1 − p + p²/2measured, dots2,048 reversed elements · 12 shuffles a pointinsertion sort
Fig. 7 Insertion sort on 2,048 reversed elements with a fraction p of positions reshuffled, twelve shuffles at each p, as a share of the unshuffled 2,096,128 comparisons: 0.9954 at p = 0.01, 0.9376 at p = 0.1, 0.8750 at p = 0.2, 0.7094 at p = 0.5 and 0.5030 at p = 1. The solid curve is 12p/3+p2/61 - 2p/3 + p^2/6, which predicts 0.7083 at p = 1/2. The dashed curve treats a mixed pair as a coin flip, 1p+p2/21 - p + p^2/2, and predicts 0.6250.

The dots sit on the solid curve at every pp — within three-tenths of a per cent up to p=1/2p = 1/2, and within one per cent at p=1p = 1, where the n1n - 1 comparisons the pair count leaves out are largest in proportion. The dashed curve, which is what the natural first guess — a disturbed pair is a coin flip — produces, is 12% too low at p=1/2p = 1/2. The checks hold insertion sort’s count to the first curve at four values of pp and require the second to be refused by the same measurements, which it is.

The curve’s slope at p=0p = 0 is the result that matters for data: two-thirds. Disturbing one per cent of a reversed array removes two-thirds of one per cent of insertion sort’s work. Nothing about the size of the input enters. The worst case is not a point with a neighbourhood but a region with a gentle floor, and what O-notation does not say about insertion sort — that its class is a statement about some inputs rather than all — applies to every input that is mostly reversed, not to one.

What each worst case warns about

The four lines on the first plate call for four different reactions, and none of them is visible in the four worst-case numbers alone.

First-element quicksort’s worst case is a warning about exact order. It is enormous — 82 times the average — and about ten positions wide. Data that is sorted exactly, or sorted except at the end, hits it in full. Data with even a few dozen displaced elements at scale has lost most of it. That is why the defences are so cheap. What randomising the pivot buys moves the pivot off the one position the peak is balanced on, and the pattern that defeats the pattern measures a deterministic version that swaps a handful of elements at fixed positions when a partition looks suspicious — which is this page’s ten positions, applied on purpose. The sort the library ships is where both ideas ended up.

Median-of-three quicksort’s worst case is a warning about an opponent. No data wanders into an input built comparison by comparison against a pivot rule, and a one per cent perturbation of that input removes most of its effect. It is a hazard exactly where somebody is constructing inputs on purpose, which is the setting the depth limit that almost never fires was designed for.

Insertion sort’s worst case is a warning about order in bulk. Its famous input is one of a broad region, and any data that runs mostly the wrong way is most of the way to it. A run is a property of the input argued that “nearly sorted” should be a measured quantity rather than a recipe; the pair count on this page is such a quantity for “nearly reversed”, and it predicts insertion sort’s cost from it exactly.

Merge sort’s worst case is not a warning about anything. A quarter of all orderings reach it at eight elements, and it is three per cent above the average at two thousand.

Still open: a sort for which every input is the worst

The widths on this page ran from ten positions to a quarter of all inputs. There is a limit further out still: a sort for which every input costs the same, so that the set of worst inputs is all of them, the distance plot is a flat line at the top, and a benchmark cannot choose a bad input because there is none to choose.

Such sorts exist and are used. A sorting network fixes in advance which pairs of positions it will compare, in which order, and never lets the answer to one comparison decide which comparison comes next. Its comparison count is a property of nn alone, identical on sorted, reversed, adversarial and random input. That removes every question this page asked — and it costs something the sorts here never pay, since a sort that cannot adapt its questions to the answers must ask enough questions for every input at once. How much more that is, measured against the enumerated distributions of the adaptive sorts, is the next measurement.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Adversarial inputBenchmark inputDistributionExhaustive searchHeapInversionsMerge sortPermutationPresortednessQuicksortSmoothed analysisWorst case