Every quadratic sort is stuck because it removes disorder one inversion at a time, so beating it needs an algorithm with a different shape entirely.
The shape
Divide and conquer is a three-step pattern. Divide the problem into smaller instances of the same problem. Conquer them by solving each recursively, stopping at a base case small enough to solve outright. Combine the sub-answers into an answer for the whole.
Binary search is already an instance, in a degenerate way: it divides into two halves, conquers one of them, and needs no combination step at all. What makes the pattern powerful is the case where both halves are solved and the combination is cheap, because then the work at each level of recursion is proportional to the data at that level, and the number of levels is logarithmic.
The reason this can beat is precisely the thing insertion sort could not do. When two sorted halves are merged, a single comparison between the fronts of the two halves can settle the relative order of one element against every remaining element of the other half at once. Each comparison carries more information than a comparison of neighbours, because the sortedness of the halves is being used.
Recurrences
A divide-and-conquer algorithm's cost is naturally written as an equation in terms of itself. If splitting into subproblems of size costs for the dividing and combining, the total is
with a base case such as . This is a recurrence, and solving it means finding a closed form for .
The first method is the recursion tree, and it is the one to reach for because it shows why the answer is what it is. Draw the root as the top-level call, costing . It has children each costing , each of which has children costing , and so on until the subproblem size reaches 1, which happens at depth . Sum the cost of each level, then sum the levels.
Take mergesort's recurrence, . The root costs . Level 1 has two nodes costing each, total . Level 2 has four nodes costing each, total . Every level costs exactly , and there are of them, so
The shape of the answer is visible in the drawing: the work per level is constant, so the total is work-per-level times number-of-levels.
Three other shapes occur. If the per-level total shrinks geometrically going down, as in , where levels cost , , , the sum is dominated by the root and . If it grows geometrically, as in , where levels cost , , , the sum is dominated by the leaves. If it stays level, as in mergesort, the logarithm appears.
Example. Solve by recursion tree, and say which algorithm it describes.
Level 0 costs 1, level 1 costs 2, level 2 costs 4, and level costs . At depth there are leaves, and the sum is dominated by the last level. So . This describes an algorithm that splits in two, recurses on both halves, and does constant work to combine: for instance finding the maximum of an array by halving, which does comparisons in total, the same as the obvious loop.
Now you. Solve , and name the algorithm.
Answer
Each level has exactly one node costing a constant, and there are levels, so . That is binary search: one subproblem of half the size, constant work to pick the midpoint and compare.
The master theorem
The recursion tree generalises into a formula, and the formula is worth having because most recurrences that arise in practice are of exactly this form. For with and , compare against the quantity , which is the total cost of the leaves.
If for some , the leaves dominate and .
If , every level costs about the same and .
If for some , and additionally for some and all large , the root dominates and .
Applied: mergesort has , , so , and matches, giving case two and . Binary search has , , so , and matches, giving . The obvious matrix multiplication algorithm splits each matrix into four blocks and does eight block multiplications plus additions: , , dominates , so case one gives , agreeing with the triple loop. Strassen's trick does seven block multiplications instead of eight, so , which is where that exponent comes from.
The theorem has gaps, and pretending otherwise causes errors. It says nothing when falls between two cases: has , and is bigger than but not by a factor of , so no case applies. (A recursion tree settles it: .) It also assumes all subproblems have the same size, which quicksort's do not, so the next lesson needs a different method entirely.
Mergesort
Now build the sort. To sort elements: if it is already sorted; otherwise split the array in half, sort each half recursively, and merge the two sorted halves into one sorted whole.
The merge is the only part with content. Keep an index into each sorted half. Compare the two elements they point at, copy the smaller into the output, and advance that index. When one half is exhausted, copy the rest of the other. Every comparison consumes one element, so merging two runs of total length costs at most comparisons and exactly moves.
Correctness of the merge is the loop invariant that the output holds, in sorted order, exactly the elements already consumed from both inputs, and that every unconsumed element is at least as large as every consumed one. That second clause is what the sortedness of the halves buys, and it is why the smaller of the two front elements is safe to emit.
The cost recurrence is , so by the tree above, and this is a worst-case bound: no input makes mergesort slow, because the split does not look at the data. Counting comparisons exactly for a power of two gives a worst case of , which is 17 for , 49 for , and about 18.9 million for a million elements. The best case, on input where every merge exhausts one side first, is , so the spread between best and worst is only a factor of two.
Mergesort is stable if the merge prefers the left run on ties, and that is the standard implementation. Its cost is memory: the merge cannot be done in place without either extra space or a much more complicated algorithm, so the usual version allocates a second array of size . In-place merging exists but the known methods are slow enough in constant factors that libraries prefer paying the memory.
Example. Merge the sorted runs 2, 5, 8, 13 and 1, 3, 9, 11. How many comparisons does it take, and what is the fewest a merge of two four-element runs could take?
Comparing fronts: 2 against 1 emits 1; 2 against 3 emits 2; 5 against 3 emits 3; 5 against 9 emits 5; 8 against 9 emits 8; 13 against 9 emits 9; 13 against 11 emits 11; and the left run's 13 is copied without a comparison. Seven comparisons, which is the maximum . The minimum is four, achieved when one run is entirely smaller than the other, as with 1, 2, 3, 4 and 5, 6, 7, 8: four comparisons exhaust the left run and the right is copied wholesale.
Now you. How many comparisons does mergesort make in the worst case on 16 elements, and how many on a million?
Answer
Using : for that is . For it is , about 18.9 million. A quadratic sort on the same million elements would use 500 billion, a factor of 26,000.
The same idea elsewhere
Divide and conquer is not a sorting technique, and the clearest evidence is a problem with no order in it at all.
Multiplying two -digit numbers by the method taught in school costs digit multiplications. In 1960 Andrey Kolmogorov conjectured in a seminar that was optimal; within a week the 23-year-old Anatoly Karatsuba had refuted him.
Split each number into halves: and with . Then
which needs four half-size multiplications: , , , . That gives , and , so case one of the master theorem returns : no gain, which is expected, since this is the school method rearranged.
Karatsuba's observation is that the middle term can be had for one multiplication rather than two, because
so , and and are already being computed. Three half-size multiplications suffice, with some extra additions, giving and .
Example. Multiply by Karatsuba, splitting each into two-digit halves.
Here , , , , and . The three products are , , and . The middle term is . Assembling, , which is the right answer, using three two-digit multiplications instead of four.
Now you. Multiply the same way.
Answer
, , , . Then , , and , so the middle term is . Assembling: .
Be honest about the size at which this pays. The extra additions and the recursion overhead mean Karatsuba loses to the school method on small numbers, and library implementations such as GMP switch over somewhere around 300 to 2500 bits depending on the machine. At 4096 bits, the size of a large RSA key, the asymptotic gap is a factor of about 32, and there it is decisive.
What mergesort still costs
Mergesort delivers a guaranteed and stability, and it charges extra memory for them. For a million records that is another eight megabytes, which is usually irrelevant; for sorting a large fraction of available memory, or on an embedded device, it is not.
The next lesson takes the opposite trade. It partitions the array in place, spending no extra memory beyond the recursion stack, and it is measurably faster than mergesort on real hardware because it moves data less and touches memory more sequentially. What it gives up is the guarantee: its worst case is quadratic, and the reason that rarely matters is a probability argument rather than a proof about the algorithm alone.