StatGardenREF. DESK
Calculators/Maths/Sorting comparisons
Maths

Sorting comparisons calculator

How many comparisons sorting n items takes, from the information-theoretic floor to what merge sort and insertion sort actually spend.

Published 1 October 2026

What this calculator does

There is a hard floor on sorting by comparison, and it comes from counting rather than from any algorithm. A list of n items has n factorial possible orderings, and each comparison has two outcomes, so k comparisons can distinguish at most 2^k arrangements. To separate all n factorial of them you need at least log base 2 of n factorial comparisons, rounded up.

For a thousand items that floor is 8,530 comparisons. Merge sort spends 8,977, which is within about five per cent of a bound that no comparison sort can ever beat. That is the useful thing this calculation tells you: the good algorithms are already close to optimal, and the room left is small.

The formula

Formulano comparison sort can beat ceil(log₂ n!); merge sort worst case is n·ceil(log₂ n) − 2^ceil(log₂ n) + 1; insertion sort worst case is n(n−1)/2

The floor is ceil(log₂ n!). Merge sort's worst case is n·ceil(log₂ n) − 2^ceil(log₂ n) + 1. Binary insertion sort inserts each item by binary search, costing the sum of ceil(log₂ k) over k, which is close to the floor but wastes the moves. Insertion sort's worst case is n(n−1)/2, every pair compared. Quicksort averages about 2n ln n, which is roughly 1.39 times the floor.

TermMeaning
Comparison sortA sort that only ever asks whether one item is before another. Counting sorts and radix sorts are not comparison sorts and are not bound by this floor.
Information-theoretic minimumceil(log₂ n!). A lower bound on the worst case, not always achievable.
Worst caseThe most comparisons the method can be forced into by an adversarial input.
Average caseWhat happens on a random input. Quicksort's worst case is quadratic, but it is rare.

The inputs explained

FieldWhat to enter
Items to sortHow many items are being sorted. Capped at 100,000 because the factorial logarithm is summed term by term.

When to use it

Judging whether an algorithm is good

Compare any method against the floor. Merge sort sits about five per cent above it at a thousand items and the gap narrows as n grows. Insertion sort at the same size spends 499,500 comparisons, nearly sixty times the floor, which is why it is only used on short lists.

Understanding why small sorts are special

At five items the floor is 7 and merge sort spends 8. That one comparison is worth chasing when the sort is a base case being run billions of times, which is exactly where AlphaDev found its improvements.

Deciding on a cutoff for hybrid sorts

Real library sorts switch to insertion sort below some size, because its quadratic comparison count is still small when n is tiny and it has almost no overhead. Comparing the columns at sizes under about 20 shows why that trade works.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

How close to the floor do real sorts get?

The list length rises by powers of ten.

Comparisons by list length
ItemsInformation-theoretic minimumMerge sort, worst caseInsertion sort, worst case
10222545
1005255734,950
1,0008,5308,977499,500
10,000118,459123,61749,995,000
100,0001,516,7051,568,9294,999,950,000
Merge sort stays within a few per cent of the floor at every size, and the gap narrows as the list grows: about 9 per cent at a hundred items and about 3.4 per cent at a hundred thousand. Insertion sort moves the other way, from roughly nine times the floor at a hundred items to over three thousand times at a hundred thousand, because it is quadratic and the floor is not.

What happens on very short lists?

Small n, where the rounding in the floor is doing real work.

The sizes that matter as base cases
ItemsInformation-theoretic minimumMerge sort, worst caseBinary insertion sort, worst case
2111
3333
4555
5788
6101111
8161717
12293333
At three items the floor is 3 and merge sort matches it. At five the floor is 7 and merge sort spends 8, and 7 is known to be achievable by a cleverer method. At twelve the floor says 29 but the true minimum is 30, which is the smallest case where the information bound is provably not reachable. Floors are not always attainable, and sorting is one of the places that shows it.

Questions

Can anything beat the information-theoretic floor?

Not by comparing. Counting sort, radix sort and bucket sort can go faster because they look at the values themselves rather than only comparing pairs, so the argument does not apply to them.

Is the floor always achievable?

No. For twelve items the floor is 29 comparisons but 30 are genuinely required, and the same happens at thirteen. The bound is a lower limit, not a promise.

Why does merge sort look worse than binary insertion sort?

On comparisons alone binary insertion sort is close to the floor, but inserting an item means shifting everything after it, so it does many more data moves. Comparisons are only one of the costs.

What did AlphaDev actually improve?

Not the comparison counts, but the machine instructions. It found shorter assembly sequences for sorting three, four and five elements, which went into the LLVM standard C++ library in 2023.

Why is quicksort used if merge sort needs fewer comparisons?

Because quicksort sorts in place with excellent memory locality, and comparisons are rarely the bottleneck on modern hardware. Its average is about 39 per cent above the floor, which is a price worth paying for the cache behaviour.

To sort an actual list there is the sort numbers calculator, and for the counting behind n factorial see permutations and combinations. How the small cases were improved is in the piece on AlphaDev.