Skip to content

recurrences, unrolled

the maths you need

what it is

Binary search on 8 numbers, for a number bigger than all of them, costs 1 look plus binary search on 4. A cost that refers to itself at a smaller size is a recurrence. Unroll it level by level, and add the levels up.

Binary search looks for a wanted number in a row in order, smallest first. Take 8 numbers and look for 96, bigger than all of them, so the search never stops early. It looks at the middle number. With two middles it takes the first, here 41. That is too small, so 96 can only come after it. The search keeps those 4 numbers and does the same on them. So the search on 8 costs 1 look plus the search on 4.

two middleslooking for 9614223741536875903 beforedropped4 afterkeptthe look, in neither sidethe other middle
8 numbers have two middles, 41 and 53. The first look takes 41, too small for 96, and keeps the 4 numbers after it.

The search on 4 is a smaller copy of the same problem, and it has a smaller copy of its own. Each size is a level: the row of 8 is level 0, the copy of 4 level 1, then 2 and 1. Each level does 1 look, at 41, 68, 75 and 90, so 4 looks in all. A cost given by the same cost at a smaller size, plus the work at this size, is a recurrence. To unroll it, write each level's work in a table, then add the levels up.

How a method makes its smaller copies is its shape. This page has three. Halving makes one copy, half the size, as binary search does. Splitting in two makes two copies, each half the size, as merge sort does. Taking one off makes one copy, one item shorter, as selection sort does.

Merge sort runs merge sort on each half of the row, then one merge joins the two. A row of 1 number is in order already. A merge joins two runs in order into one. While both have numbers left, it places the smaller front number next. Once one is used up, the rest of the other follows. Each number placed is a placing. So merge sort on 8 costs two merge sorts on 4 plus 8 placings. Selection sort takes the first number as the smallest found so far. It compares each other number with the smallest so far, keeping the smaller, then swaps the smallest to the front. So selection sort on 5 costs 4 comparisons plus selection sort on the 4 after the front.

Halving and splitting in two use rows of 2n2^n items, so every halving comes out exact. 2n2^n, read 2 to the power nn, is nn twos multiplied together: 8 is 2 to the power 3. Each halving takes one 2 away, so 2n2^n items halve nn times down to 1, and half of 2 is 1, so 2 to the power 0 is 1. Taking one off works on rows of any length, so there nn counts the items, not the halvings. On nn items it costs 1+2+⋯+(n−1)1 + 2 + \dots + (n - 1), the adding up that stops one short of nn, which is (n−1)n2\frac{(n - 1)n}{2}.

shapemethodcopies it passes downwork at a levelin all
halvingbinary search, for a number bigger than allone, half the size1 lookn+1n + 1, on 2n2^n items
splitting in twomerge sorttwo, each half the sizea placing for each item, at each level that mergesn×2nn \times 2^n, on 2n2^n items
taking one offselection sortone, one item shorter1 comparison fewer than its items(n−1)n2\frac{(n - 1)n}{2}, on nn items
The three shapes, each unrolled in a question below. In the first two nn counts halvings, in the last items.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1Each level halves the input and does one step. What does an input of 2n2^n items cost?
  1. the move: unroll it into levels

    the line:
    level j holds one copyof 2n−j items, doing 1 step\begin{gathered} \text{level } j \text{ holds one copy} \\ \text{of } 2^{n - j} \text{ items, doing 1 step} \end{gathered}
    Binary search on 8, unrolled. Levels 1 to 3 keep the half after the look above. Dashed, that look and the numbers before it. Each level does 1 look, 4 in all.
    levelcopiesitems in eachlooks at this level
    0181
    1141
    2121
    3111
    in all4
    The levels for 8 items, 2n2^n with nn as 3.

    why it is allowed: Let jj be a level's number, 0 at the top. After jj halvings, n−jn - j twos are left, so level jj holds 2n−j2^{n - j} items. For 8, level 1 holds 4, which is 2 to the power 2. The levels stop at level nn, with no 2 left: 2 to the power 0 is 1 item. Each level's step is one look, and as in the first picture each look before the last keeps exactly the half after it.

  2. the move: add up the levels

    the line:
    H=∑j=0n1H = \sum_{j=0}^{n} 1

    why it is allowed: Let HH be the cost, the looks of every level added. Σ\Sigma, read the sum of, adds the term after it, here 1, once for each jj, from 0 under the sign to nn over it. For 8, nn is 3, so it is 1 + 1 + 1 + 1.

  3. the move: count the levels

    the line:
    H=n+1H = n + 1
    numberlog× 2+ 1833422211100
    8 halves to 4, 2 and 1. That is 3 halvings, so 4 levels and 4 looks.

    why it is allowed: The levels run from 0 to nn, which is n+1n + 1 of them, each adding 1. For 8, 4 looks.

  4. the move: read it

    the line:
    one more step thanthe halvings\begin{gathered} \text{one more step than} \\ \text{the halvings} \end{gathered}

    why it is allowed: By line 3 the cost is one more than the halvings. The page 'halving and logarithms' names that count the log to base 2 of the size. The number line at line 3 writes it under each number, 3 under the 8. Doubling to 16 items adds one halving, so one look: 5 against 4.

try it with a small nn: at n=3n = 3, line 2 gives H=4H = 4, which you can work out by hand

question 2Each level splits in two and does a step for each item it merges. What does an input of 2n2^n items cost?
  1. the move: unroll it into levels

    the line:
    level j holds 2j copiesof 2n−j items each\begin{gathered} \text{level } j \text{ holds } 2^j \text{ copies} \\ \text{of } 2^{n - j} \text{ items each} \end{gathered}
    the row: 47, 12, 85, 30, 61, 9, 74, 2611111111start2222round 144round 28round 32a run, its length insidethe first runall in one runa merge in round 3, its numbers123047859266174912263047617485goes straight in, the other run used up
    The site's merge sort on 8 numbers, runs of 1, then 2, 4 and 8. Rounds 1, 2 and 3 make the copies of levels 2, 1 and 0. Its last merge places all 8.
    levelcopiesitems in eachplacings at this level
    0188
    1248
    2428
    3810
    in all24
    The levels for 8 items, 2n2^n with nn as 3.

    why it is allowed: Each copy splits in two, so the copies double at each level: 2j2^j copies at level jj. Each holds 2n−j2^{n - j} items, as in question 1. Cutting a row costs nothing here; only placings count. Each copy's merge places each of its items once. A copy of 1 item merges nothing, so the levels that merge run from 0 to n−1n - 1.

  2. the move: the work at a level

    the line:
    level j places 2j×2n−jitems, for j from 0 to n−1\begin{gathered} \text{level } j \text{ places } 2^j \times 2^{n - j} \\ \text{items, for } j \text{ from 0 to } n - 1 \end{gathered}
    Merge sort on 8, unrolled. Each copy of 2 or more splits into the two under it. Each level that merges places all 8, and the copies of 1 place none, 24 in all.

    why it is allowed: By line 1, level jj holds 2j2^j copies, each placing its 2n−j2^{n - j} items once. For 8, level 1 holds 2 copies of 4, so 2 times 4, 8 placings.

  3. the move: add up the levels that merge

    the line:
    M=∑j=0n−12j×2n−jM = \sum_{j=0}^{n - 1} 2^j \times 2^{n - j}

    why it is allowed: Let MM be the cost, the placings of every level that merges added. When nn is 3, jj as 0, 1 and 2 gives 1 times 8, 2 times 4 and 4 times 2. That is a term for each row of the table that merges, 8 + 8 + 8.

  4. the move: each level places the whole input

    the line:
    M=∑j=0n−12nM = \sum_{j=0}^{n - 1} 2^n

    why it is allowed: jj twos times n−jn - j more twos make nn twos. So each level places 2n2^n, the whole input, whatever jj is.

  5. the move: count the levels that merge

    the line:
    M=n×2nM = n \times 2^n

    why it is allowed: The levels that merge run from 0 to n−1n - 1, which is nn of them, each adding 2n2^n. For 8, 3 times 8 is 24.

  6. the move: read it

    the line:
    the size timesits log to base 2\begin{gathered} \text{the size times} \\ \text{its log to base 2} \end{gathered}

    why it is allowed: 2n2^n is the size and nn is its log to base 2. So by line 5 the cost is the size times its log, 24 placings for 8 items, as the site's merge sort counts them.

try it with a small nn: at n=3n = 3, line 3 gives M=24M = 24, which you can work out by hand

question 3Each level takes one off and does one step fewer. What does an input of nn items cost?
  1. the move: unroll it into levels

    the line:
    a level of m items makesm−1 comparisons\begin{gathered} \text{a level of } m \text{ items makes} \\ m - 1 \text{ comparisons} \end{gathered}
    the row15, 24, 38, 62, 7
    after pass 17, 24, 38, 62, 15
    after pass 27, 15, 38, 62, 24
    after pass 37, 15, 24, 62, 38
    after pass 47, 15, 24, 38, 62
    The site's selection sort on 15, 24, 38, 62, 7. Each pass swaps the smallest of the rest to its front, leaving a row one shorter.
    Selection sort on 5, unrolled, each level named by its items. The level of 4 is the row after pass 1, less its settled front in blue, and so on. The levels compare 4, 3, 2, 1 and 0, 10 in all.

    why it is allowed: Here nn is the number of items, not a count of halvings. Name a level by its items, mm, from nn down to 1. At a level of mm items, each of the m−1m - 1 after the first is compared once with the smallest so far. The level of 1 item compares nothing. So n−1n - 1 levels work, 4 for 5 items.

  2. the move: add up the levels from the smallest

    the line:
    C=1+2+⋯+(n−1)C = 1 + 2 + \dots + (n - 1)

    why it is allowed: Let CC be the cost, the comparisons of every level added, for nn at least 2. By line 1 the level of 2 makes 1, the level of 3 makes 2, and on, up to n−1n - 1 at the level of nn. For 5, 1 + 2 + 3 + 4.

  3. the move: use the page on adding up, reading its n as n - 1

    the line:
    C=(n−1)((n−1)+1)2C = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    Two staircases of 1 to 4 squares, yellow hanging and blue standing, fill a rectangle 4 wide and 5 tall, 20 squares. So one staircase, 1 + 2 + 3 + 4, is 10.
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The page on adding up finds that the whole numbers from 1 up to any last number add up to that number times one more than it, halved. Here the sum stops at n−1n - 1. So read that page's nn as this page's n−1n - 1. For 5 items the sum is 1 + 2 + 3 + 4, that page's sum with its nn as 4. And 4 times 5, halved, is 10.

  4. the move: tidy the bracket

    the line:
    C=(n−1)n2C = \frac{(n - 1)n}{2}

    why it is allowed: (n−1)+1(n - 1) + 1 is nn. For 5, 4 times 5, halved, is 10.

  5. the move: multiply out the bracket

    the line:
    C=n2−n2C = \frac{n^2 - n}{2}

    why it is allowed: n−1n - 1 lots of nn are nn lots of nn, less one nn. That is n2−nn^2 - n, where n2n^2, read nn squared, is nn times nn. For 5, 4 times 5 is 20, and 25 less 5 is 20 too.

  6. the move: halve each part

    the line:
    C=n22−n2C = \frac{n^2}{2} - \frac{n}{2}

    why it is allowed: Half of a difference is the difference of the halves. For 5, half of 20 is 10, and 12.5 less 2.5 is 10 too.

  7. the move: take out the biggest part

    the line:
    C=n22×(1−1n)C = \frac{n^2}{2} \times \Bigl(1 - \frac{1}{n}\Bigr)
    comparisonsthe items05010015020005101520comparisonsthe items05010015020005101520
    • the comparisons, (n−1)n2\frac{(n - 1)n}{2}
    • half of the items squared, n22\frac{n^2}{2}
    From 1 to 20 items, the comparisons sit just under half the items squared. The gap is half the items, so at 20 it is 190 against 200.
    itemsCChalf of the items squaredCC divided by it
    51012.50.8
    1045500.9
    1004,9505,0000.99
    1,000499,500500,0000.999
    Selection sort's comparisons beside half of the items times the items. The last column, 1 take away 1 over the items, climbs towards 1.

    why it is allowed: n22\frac{n^2}{2} is n22\frac{n^2}{2} times 1, and n2\frac{n}{2} is n22\frac{n^2}{2} times 1 over nn. Multiplying spreads over a difference, line 6 backwards, so take n22\frac{n^2}{2} out of both parts. For 5, 12.5 less 2.5 is 12.5 times 0.8, which is 10. The bracket is CC divided by n22\frac{n^2}{2}, the last column of the table.

  8. the move: read it

    the line:
    about the size timesitself, halved\begin{gathered} \text{about the size times} \\ \text{itself, halved} \end{gathered}

    why it is allowed: By line 7, as nn grows, 1 over nn shrinks towards nothing, 0.01 at 100. So for a big input the cost is about the size times itself, halved: 4,950 for 100 items, close to 5,000. For 5 items the part taken away still counts, 10 against 12.5.

try it with a small nn: at n=5n = 5, line 2 gives C=10C = 10, which you can work out by hand

a worked example

Unroll merge sort's cost for 16 items, level by level.

  1. 16 is 2n2^n with nn as 4, so the levels run from 0 to 4. Level 4 holds copies of 1 item, which merge nothing.
  2. Levels 0 to 3 hold 1 copy of 16, 2 of 8, 4 of 4 and 8 of 2. Each level places all 16.
  3. Four levels of 16 make 64. And n×2nn \times 2^n with nn as 4 is 4 times 16, 64 too.

the trap

check yourself

  1. Each level halves the input and does one step. How many steps for 64 items?

    the answer

    64 is 2n2^n with nn as 6. By question 1 that is n+1n + 1, 7 steps.

  2. How many placings does merge sort make on 1,024 items?

    the answer

    1,024 is 2n2^n with nn as 10. By question 2 that is n×2nn \times 2^n, 10 times 1,024, which is 10,240.

  3. How many comparisons does selection sort make on 30 numbers?

    the answer

    By question 3, 29 times 30, halved, which is 435.