Skip to content

Binary search

finding

Level 1 of 3

Also called Half-interval search and Logarithmic search.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Binary search looks for 46 in 5, 14, 20, 33, 39, 46, 53, 64, 71. At which place is its second look?

51420333946536471
the answer, and why

the answer is B

The first look is at the middle, place 5, which holds 39. 46 is bigger, so places 6 to 9 are left. Their middles are places 7 and 8, and it takes the first.

  • Looking for 46: 3 looks, found at place 6.

    51142203334395146635372648719
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • ruled out without a look
Looks at places 5, 7 and 6. The third look finds 46.

if you chose another

  • 46 stands at place 6, but the second look lands at place 7, before 46 is reached.
  • Place 8 is the second of the two middles. This binary search takes the first.
  • Place 3 is in the half the first look ruled out, since 46 is bigger than 39.

question 2

Linear search would stop at the first 17, place 2. At which place does binary search stop, looking for 17 in 9, 17, 17, 17, 30, 44, 52?

9171717304452
the answer, and why

the answer is B

Its first look is at the middle, place 4, which holds 17. It stops there, at the first middle holding the number.

  • Looking for 17: 1 look, found at place 4.

    911721731741305446527
  • a look, numbered in turn
  • found, ringed
  • never reached
The first look, at place 4, finds 17. Places 2 and 3 are never looked at.

if you chose another

  • That is linear search's answer, and bisect_left's, which gives index 1. Binary search never looks at place 2 here.
  • Place 3 is never looked at, since the first look already finds 17.
  • Place 1 holds 9, which is not 17.

question 3

Which search costs binary search the most looks?

the answer, and why

the answer is C

55 stands at place 7, on the bottom level of this row's tree, 4 looks. 47 takes 3, 13 takes 2, and 34, the middle, 1.

Looking for 55: 4 looks, found at place 7.

level 1level 2level 3level 46132128344755627891611322132843454765576287899110

How many places on each level, from the top: 1, 2, 4, 3. Each level down costs one look more.

  • looked at, joined by heavy lines
  • found, ringed
The row as a tree of looks. 55 is on level 4, the fourth look.

if you chose another

  • 13 is a middle of the part before 34, the second look.
  • 47 is on level 3, 3 looks.
  • 34 is the middle, found at the first look.

question 4

Which is true of binary search's looks?

the answer, and why

the answer is C

The middle costs 1 look, Θ(1)\Theta(1). A number bigger than all costs ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1, Θ(log⁡2 n)\Theta(\log_2\, n).

this tests best, worst and average, on the learn page

if you chose another

  • A ceiling is not a tight bound. The middle still costs 1 look.
  • In a row of 12, a number smaller than all of them costs 3 looks, one fewer than the most.
  • Doubling the row adds one halving, so one look.

pen and paper

question 5about 10 minutes

Binary search looks for a number that is in the row 2, 10, 17, 23, 29, 36, 41, 49, 54, 58, 65, 72, 80, 87, 93, each place as likely.

  1. How many places stand on each level of its tree of looks?
  2. Add the looks over every place, then average them.
  3. Check your average against the formula, with hh as 3.
a hint

Each level holds twice the places of the level above, until the row runs out.

a worked answer

The levels hold 1, 2, 4 and 8 places, costing 1, 4, 12 and 32 looks: 49 over 15, about 3.27. The formula, with hh as 3, gives 4 times 16, which is 64, less 16, plus 1, which is 49, over 15, so 49 over 15 too.

levelplaces on itlooks eachlooks in all
1111
2224
34312
48432
in all1549
The 15 numbers level by level, 1, 2, 4 and 8 places on levels 1 to 4.
Why does a number that is there cost (h+1)(n+1)−2h+1+1n\frac{(h + 1)(n + 1) - 2^{h + 1} + 1}{n} looks on average, each place as likely?
  1. the move: price one place

    the line:
    a place on level dcosts d looks\begin{gathered} \text{a place on level } d \\ \text{costs } d \text{ looks} \end{gathered}
    level 1level 2level 3512203344576451122203334445576647

    How many places on each level, from the top: 1, 2, 4. Each level down costs one look more.

    7 different numbers as a tree of looks. 33 costs 1 look, 12 and 57 cost 2, the four on the bottom cost 3.
    levelplaces on itlooks eachlooks in all
    1111
    2224
    34312
    in all717
    Added level by level, 17 looks over 7 places.

    why it is allowed: Level dd holds the places a search reaches at its look number dd; the middle is level 1. Here every number is different: copies would stop some searches sooner.

  2. the move: compare the parts

    the line:
    parts on one leveldiffer by 1 at most\begin{gathered} \text{parts on one level} \\ \text{differ by 1 at most} \end{gathered}
    level 1level 2level 3level 46132128344755627891611322132843454765576287899110

    How many places on each level, from the top: 1, 2, 4, 3. Each level down costs one look more.

    10 numbers, 1, 2 and 4 places on the full levels, then 3 of a possible 8 on the last.
    levelpartstheir sizesplaces on it
    11101
    224 and 52
    341, 2, 2 and 24
    480, 0, 0, 1, 0, 1, 0 and 13
    On 10 numbers the sizes on each level differ by 1 at most. A level holds a place for each part that is not empty.

    why it is allowed: The aim is to show only the last level can be part full. A look takes its part's middle and splits the rest into two halves, rounded down and up, which differ by 1 at most. Two parts that differ by 1 split into four that still do.

  3. the move: fill the levels

    the line:
    levels 1 to h hold 1,2,…,2h−1places, level h+1 the rest\begin{gathered} \text{levels 1 to } h \text{ hold } 1, 2, \dots, 2^{h - 1} \\ \text{places, level } h + 1 \text{ the rest} \end{gathered}

    why it is allowed: On 10, hh is 3: levels 1 to 3 are full, 1, 2 and 4 places, and level 4 holds the other 3. Level 4's parts hold 0 or 1, so there is no level 5, and only the last level can be part full. That last level is h+1h + 1, 4 for 10. No search looks more, by the worst case's lines 6 and 7. The search for the last number goes right at every look until it finds it, as one bigger than all does, so its look h+1h + 1 lands on it.

  4. the move: count level by level

    the line:
    A=1n∑j=0h(n−(1+2+⋯+2j−1))\begin{aligned} A = {} & \frac{1}{n} \sum_{j=0}^{h} \bigl(n \\ & - (1 + 2 + \dots + 2^{j - 1})\bigr) \end{aligned}
    6132128344755627891dotslevel 110level 29level 37level 43looks323413423429

    Down each column, that place's looks; across each row, the places whose search reaches that level. Both add to 29.

    The 10 numbers of line 2, a dot for each look. A row's empty cells are the places above it. Down the columns or across the rows, the dots add to 29.
    leveljjplaces aboveplaces paying
    100, none10
    2119
    321 + 2, which is 37
    431 + 2 + 4, which is 73
    in all29
    The 10 numbers again. 29 looks over 10 places is 2.9.

    why it is allowed: AA is the average, every place's looks added, over nn. Each place pays a look on every level down to its own. Here jj counts the full levels above a level, not halvings. That level is paid by every place but the 1 + 2 + ... + 2j−12^{j - 1} above it. The sum stops at 2j−12^{j - 1}, so at jj as 1 it is just 1, and at jj as 0 nothing, as the grid's rows show.

  5. the move: use the doubling sum, j for n

    the line:
    A=1n∑j=0h(n−(2j−1))A = \frac{1}{n} \sum_{j=0}^{h} \bigl(n - (2^j - 1)\bigr)
    1, 2 and 4 added a row at a time. Each total, plus the 1 hollow square, makes the next term.
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

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

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term, here with jj for its nn. For 3 levels above, 1 + 2 + 4 is 7, one less than 8.

  6. the move: tidy each term

    the line:
    A=1n∑j=0h((n+1)−2j)A = \frac{1}{n} \sum_{j=0}^{h} \bigl((n + 1) - 2^j\bigr)

    why it is allowed: Taking away 1 less than a number is taking the number away and adding 1 back. For 10 and 8, 10 less 7 is 10 less 8, plus 1, which is 3.

  7. the move: split the sum

    the line:
    A=1n(∑j=0h(n+1)−∑j=0h2j)A = \frac{1}{n} \Bigl(\sum_{j=0}^{h} (n + 1) - \sum_{j=0}^{h} 2^j\Bigr)

    why it is allowed: Add up the n+1n + 1s and the powers of 2 apart. For 10, 11 + 11 + 11 + 11 is 44; take away 1 + 2 + 4 + 8, which is 15: 29.

  8. the move: gather the terms

    the line:
    A=(h+1)(n+1)−(1+2+⋯+2h)nA = \frac{\begin{gathered} (h + 1)(n + 1) \\ - (1 + 2 + \dots + 2^h) \end{gathered}}{n}

    why it is allowed: jj runs from 0 to hh, so each sum has h+1h + 1 terms, which make h+1h + 1 lots of n+1n + 1, less the powers of 2.

  9. the move: use the doubling sum, h for n

    the line:
    A=(h+1)(n+1)−(2h+1−1)nA = \frac{(h + 1)(n + 1) - (2^{h + 1} - 1)}{n}
    where does this come from?
    question 1 of the page doubling and halving sumsAdd 1+2+4+⋯+2n1 + 2 + 4 + \dots + 2^n. What is the total?
    1. the move: name the sum

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

      why it is allowed: Let SS be the doubling sum up to 2n2^n. For nn as 3, it is 1 + 2 + 4 + 8.

    2. the move: double both sides

      the line:
      2S=2+4+8+⋯+2n+12S = 2 + 4 + 8 + \dots + 2^{n + 1}

      why it is allowed: Each term doubled is the next one along, and 2n2^n becomes 2n+12^{n + 1}. Doubling both sides keeps them equal. The plan: the two sums share all but their ends.

    3. the move: take line 1 away

      the line:
      2S−S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} 2S - S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Take SS from both sides of line 2. On the right, write SS out as line 1 does.

    4. the move: tidy the left side

      the line:
      S=(2+4+⋯+2n+1)−(1+2+⋯+2n)\begin{aligned} S = {} & (2 + 4 + \dots + 2^{n + 1}) \\ & - (1 + 2 + \dots + 2^n) \end{aligned}

      why it is allowed: Two lots of SS less one lot is one lot.

    5. the move: cancel what both brackets share

      the line:
      S=2n+1−1S = 2^{n + 1} - 1
      when n is 3124816
      first bracket24816
      second bracket1248
      left overtake away 116
      Line 4 when nn is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.

      why it is allowed: Every term from 2 to 2n2^n is in both brackets and cancels, leaving 2n+12^{n + 1} less 1.

    6. the move: the terms before the last

      the line:
      1+2+⋯+2n−1=2(n−1)+1−1\begin{gathered} 1 + 2 + \dots + 2^{n - 1} \\ = 2^{(n - 1) + 1} - 1 \end{gathered}

      why it is allowed: The sum of line 5 stopping one term sooner: line 5 with n−1n - 1 for nn. For 3, it is 1 + 2 + 4. At nn as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.

    7. the move: tidy the power

      the line:
      2(n−1)+1−1=2n−12^{(n - 1) + 1} - 1 = 2^n - 1

      why it is allowed: One more than n−1n - 1 is nn. For 3, 1 + 2 + 4 is 7, one less than 8.

    8. the move: read it

      the line:
      one less thanthe next term,under twice the last\begin{gathered} \text{one less than} \\ \text{the next term,} \\ \text{under twice the last} \end{gathered}

      why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under 2n+12^{n + 1}, twice the last.

    the whole page: doubling and halving sums

    why it is allowed: The same box, with hh for its nn, the doubling sum up to 2h2^h is 2h+12^{h + 1} less 1. For 10, hh is 3, and 1 + 2 + 4 + 8 is 15.

  10. the move: tidy

    the line:
    A=(h+1)(n+1)−2h+1+1nA = \frac{(h + 1)(n + 1) - 2^{h + 1} + 1}{n}

    why it is allowed: As in line 6. For 10, 4 times 11, less 16, plus 1, is 29, and 29 over 10 is 2.9.

  11. the move: a ceiling from the most looks

    the line:
    A≤⌊log⁡2 n⌋+1A \le \lfloor \log_2\, n \rfloor + 1

    why it is allowed: No place costs more than the most looks, by the worst case's lines 6 and 7, so neither does the average.

  12. the move: drop the rounding

    the line:
    A≤log⁡2 n+1A \le \log_2\, n + 1

    why it is allowed: By line 11, as rounding down never makes a number bigger.

  13. the move: a fixed multiple

    the line:
    A≤2 log⁡2 n for n≥2A \le 2\,\log_2\, n \text{ for } n \ge 2

    why it is allowed: From 2 numbers on, 1 is at most log⁡2 n\log_2\, n.

  14. the move: read it as a bound

    the line:
    A∈O(log⁡2 n)A \in O(\log_2\, n)

    why it is allowed: By line 13, with the fixed multiple 2.

  15. the move: a floor

    the line:
    A≥(h+1)(n+1)−2n+1nA \ge \frac{(h + 1)(n + 1) - 2n + 1}{n}

    why it is allowed: From line 10. 2h2^h is at most nn, as hh is log⁡2 n\log_2\, n rounded down, so 2h+12^{h + 1} is at most 2n2n, and taking away 2n2n takes at least as much. For 10, 8 is at most 10, so 16 is at most 20.

  16. the move: multiply out

    the line:
    A≥(h+1)n+(h+1)−2n+1nA \ge \frac{(h + 1)n + (h + 1) - 2n + 1}{n}

    why it is allowed: h+1h + 1 times n+1n + 1 is h+1h + 1 lots of nn, and h+1h + 1 more. For 10, 44 less 20, plus 1, is 25, so 2.5, under the true 2.9.

  17. the move: gather

    the line:
    A≥(h−1)n+h+2nA \ge \frac{(h - 1)n + h + 2}{n}

    why it is allowed: h+1h + 1 lots of nn, less 2 lots, leave h−1h - 1 lots, and h+1h + 1, plus 1, is h+2h + 2. For 10, 25 over 10.

  18. the move: share out

    the line:
    A≥(h−1)+h+2nA \ge (h - 1) + \frac{h + 2}{n}

    why it is allowed: Share each part of line 17 by nn. h−1h - 1 lots of nn, over nn, is h−1h - 1. For 10, 2 plus 5 over 10, which is 2.5.

  19. the move: drop the part over n

    the line:
    A≥h−1A \ge h - 1

    why it is allowed: h+2h + 2 over nn is more than 0, so dropping it leaves less. So the average is within 2 looks of the most, as 3.08 against 4 on 12 numbers.

  20. the move: a third of h + 1

    the line:
    A≥h+13 for n≥4A \ge \frac{h + 1}{3} \text{ for } n \ge 4
    hhh+13\frac{h + 1}{3}h−1h - 1
    211
    3about 1.332
    4about 1.673
    524
    From hh as 2, a third of h+1h + 1 grows by a third each step, and h−1h - 1 by 1.

    why it is allowed: The aim is a fixed multiple of log⁡2 n\log_2\, n under AA. A third, because at hh as 2 both sides are 1, and h−1h - 1 grows faster after, as the table shows. From 4 numbers on, hh is at least 2.

  21. the move: a simpler floor

    the line:
    A≥log⁡2 n3 for n≥4A \ge \frac{\log_2\, n}{3} \text{ for } n \ge 4

    why it is allowed: log⁡2 n\log_2\, n is less than h+1h + 1. At 4, line 20 gives 1, this line two thirds, and AA is 2.

  22. the move: read it as a bound

    the line:
    A∈Ω(log⁡2 n)A \in \Omega(\log_2\, n)

    why it is allowed: By line 21, with the fixed multiple a third.

  23. the move: ceiling and floor together

    the line:
    A∈Θ(log⁡2 n)A \in \Theta(\log_2\, n)

    why it is allowed: Lines 14 and 22 bound AA by the same growth, log⁡2 n\log_2\, n, with the fixed multiples 2 and a third.

try it on small inputs: over the 7 places of a row of 7 different numbers, each tried once, the average AA is about 2.43

this tests where the formula comes from, on the learn page

what a full answer includes
  • The four levels, 1, 2, 4 and 8 places.
  • The total 49, and 49 over 15, about 3.27.
  • The formula giving 49 over 15 too.

question 6about 10 minutes

A row of 20 numbers in order. Binary search looks for a number bigger than all of them, then for one smaller than all.

  1. Write how many numbers each call searches, for the bigger number, until none is left.
  2. Count the looks, and check them against ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1.
  3. Do the same for the smaller number. Why is it one look fewer?
a hint

After its middle, a part of nn keeps ⌊n/2⌋\lfloor n / 2 \rfloor. Before it stand one fewer when nn is even.

a worked answer

Bigger, 20, 10, 5, 2, 1, then none: 5 looks. 20 lies between 16 and 32, so its log lies between 4 and 5. Rounded down that is 4, and 4 and 1 more is 5. Smaller, 20, 9, 4, 1, then none: 4 looks. The middle taken is the first of two, so the part before it is the smaller one.

lookbigger, its part holdssmaller, its part holds
12020
2109
354
421
51none left
The part each look searches. The bigger number needs 5 looks, the smaller 4.
Why does a number bigger than every number cost ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1 looks?
  1. the move: watch the search

    the line:
    a call looks once, thencalls itself on the part kept\begin{gathered} \text{a call looks once, then} \\ \text{calls itself on the part kept} \end{gathered}
    • Looking for 95: 4 looks, not there.

      6113221328434514765576282789391104
    • a look, numbered in turn
    • looked at, not it
    • ruled out without a look
    95 is bigger than all 10 numbers. The calls look at places 5, 8, 9 and 10, each ruling out the side before.

    why it is allowed: A call is one round: one look at the middle of its part. Unless that is the number, it rules out one side and calls itself on the other.

  2. the move: bigger than all

    the line:
    each call keeps the⌊n/2⌋ after its middle\begin{gathered} \text{each call keeps the} \\ \lfloor n / 2 \rfloor \text{ after its middle} \end{gathered}
    callits part holdsits middlekept after itlooks so far
    110place 551
    25place 822
    32place 913
    41place 1004
    50none, so no look4
    The calls for 95. Each part keeps half of the one before, rounded down, 10, 5, 2, 1, then 0.

    why it is allowed: A part of nn numbers takes the first of two middles, so half, rounded down, stand after it, ⌊n/2⌋\lfloor n / 2 \rfloor. A number bigger than all keeps that side.

  3. the move: write the calls as a cost

    the line:
    n numbers: 1 look,then the call on ⌊n/2⌋0 numbers: no look\begin{gathered} n \text{ numbers: 1 look,} \\ \text{then the call on } \lfloor n / 2 \rfloor \\ \text{0 numbers: no look} \end{gathered}

    why it is allowed: The looks on nn numbers are 1, plus the looks on ⌊n/2⌋\lfloor n / 2 \rfloor. For 10, 1 plus the looks on 5, then 1 + 1 plus those on 2, and on: line 2's last column. A cost given by the same cost at a smaller size is a recurrence.

  4. the move: unroll it

    the line:
    call j+1 holds n halvedj times, rounding down\begin{gathered} \text{call } j + 1 \text{ holds } n \text{ halved} \\ j \text{ times, rounding down} \end{gathered}
    numberlog× 2+ 14283164103.32352.322211100
    Top, numbers, each double the last; bottom, their logs, each 1 more. 10 halves to 5, 2 and 1, rounding down, 3 halvings. 10 lies between 8 and 16, so its log lies between 3 and 4, and rounds down to 3. In blue, each log rounded down, one less at each halving.

    why it is allowed: Here jj counts the halvings so far, so call 1, with jj as 0, holds the whole row. Each call looks once while its part holds a number. For 10, calls 1 to 5 hold 10, 5, 2, 1 and 0.

  5. the move: add up the calls

    the line:
    W=∑j=0h1W = \sum_{j=0}^{h} 1

    why it is allowed: WW is the looks for a number bigger than all, and hh the halvings, rounding down, that bring nn to 1. By line 4, the calls with jj from 0 to hh each look once, and the next holds none. Σ\Sigma adds the 1 after it once for each jj from 0 to hh. For 10, hh is 3.

  6. the move: use the page on halving: its count of halvings is our h

    the line:
    W=∑j=0⌊log⁡2 n⌋1W = \sum_{j=0}^{\lfloor \log_2\, n \rfloor} 1
    where does this come from?
    question 3 of the page halving and logarithmsBinary search on a row of nn numbers. At most how many looks?
    1. the move: what one look keeps

      the line:
      at most half, rounded down,of what is left\begin{gathered} \text{at most half, rounded down,} \\ \text{of what is left} \end{gathered}
      two middleslooking for 7510203040506070803 beforedropped4 afterkeptthe look, in neither sidethe other middle
      8 numbers have two middles; the table's 7 have one.
      lookstill to searchhalf, rounded downthe middlekept
      110, 20, 30, 40, 50, 60, 703.5 to 34050, 60, 70
      250, 60, 701.5 to 16070
      3700.5 to 070none
      75 is above all 7 numbers, so each look keeps the side after.

      why it is allowed: This site's binary search looks at the middle of what is left, the first of two middles, and keeps the side the wanted number must be on. From 8 it keeps 3 before or 4 after: the side after holds half, rounded down, the side before no more.

    2. the move: fewer numbers, no more looks

      the line:
      keeping a smaller part nevercosts more looks\begin{gathered} \text{keeping a smaller part never} \\ \text{costs more looks} \end{gathered}
      after lookkeeping the bigger sidekeeping the smaller side
      01010
      154
      221
      31none
      4none
      Two searches on 10 numbers. Keeping the bigger side runs out after 4 looks, the smaller after 3.

      why it is allowed: Let A always keep the bigger side, and S be any search on the same row. While S holds no more than A, it keeps at most half of its part, rounded down, so no more than A keeps. In the table, S keeps 4 to A's 5, then 1 to 2. So S runs out no later.

    3. the move: a number that keeps the bigger side

      the line:
      a number above them allkeeps the bigger side\begin{gathered} \text{a number above them all} \\ \text{keeps the bigger side} \end{gathered}

      why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.

    4. the move: count that search's looks

      the line:
      ⌊log⁡2 n⌋ looks leaveone number, one morelook leaves none\begin{gathered} \lfloor \log_2\, n \rfloor \text{ looks leave} \\ \text{one number, one more} \\ \text{look leaves none} \end{gathered}

      why it is allowed: Each look halves, rounding down, so question 2 of the page halving and logarithms counts the looks to one number. 1 number takes no halving, log 0. For 7, 7 leaves 3, 3 leaves 1, and look 3 leaves none.

    5. the move: the most looks

      the line:
      B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1

      why it is allowed: Let BB be the most looks, which lines 2 to 4 count. For 7 numbers, the search for 75 takes 3, as the table at line 1 shows.

    6. the move: read it

      the line:
      one more look thanthe log rounded down\begin{gathered} \text{one more look than} \\ \text{the log rounded down} \end{gathered}

      why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.

    the whole page: halving and logarithms

    why it is allowed: The box counts these halvings, keeping the bigger side, so hh is ⌊log⁡2 n⌋\lfloor \log_2\, n \rfloor, and shows no search makes more looks.

  7. the move: count the calls, h for n

    the line:
    W=⌊log⁡2 n⌋+1W = \lfloor \log_2\, n \rfloor + 1
    numbershalved down to 1calls that lookhh
    88, 4, 2, 143
    1010, 5, 2, 143
    1515, 7, 3, 143
    1616, 8, 4, 2, 154
    Rounding down changes the calls' sizes, not their number. 8, 10 and 15 all take 4 calls; 16 is the first to take 5.
    where does this come from?
    question 1 of the page recurrences, unrolledEach 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.

    the whole page: recurrences, unrolled

    why it is allowed: The calls with jj from 0 to hh are one more than hh, 1 look each. For 10, hh is 3, so WW is 1 + 1 + 1 + 1, which is 4. The box under this line counts the same shape on rows that halve exactly, its nn being this page's hh.

  8. the move: drop the rounding

    the line:
    W≤log⁡2 n+1W \le \log_2\, n + 1

    why it is allowed: By line 7, as rounding down never makes a number bigger.

  9. the move: a fixed multiple

    the line:
    W≤2 log⁡2 n for n≥2W \le 2\,\log_2\, n \text{ for } n \ge 2
    nnlog⁡2 n\log_2\, nWW2 log⁡2 n2\,\log_2\, n
    2122
    4234
    10about 3.324about 6.64
    16458
    1,000about 9.9710about 19.93
    WW stands between log⁡2 n\log_2\, n and twice it, from 2 numbers on.

    why it is allowed: From 2 numbers on, log⁡2 n\log_2\, n is at least 1, so the 1 of line 8 is at most log⁡2 n\log_2\, n.

  10. the move: read it as a bound

    the line:
    W∈O(log⁡2 n)W \in O(\log_2\, n)

    why it is allowed: By line 9, with the fixed multiple 2.

  11. the move: a floor

    the line:
    W≥log⁡2 nW \ge \log_2\, n

    why it is allowed: By line 7. Rounding down takes off less than 1, and line 7 adds 1 back.

  12. the move: read it as a bound

    the line:
    W∈Ω(log⁡2 n)W \in \Omega(\log_2\, n)

    why it is allowed: By line 11.

  13. the move: ceiling and floor together

    the line:
    W∈Θ(log⁡2 n)W \in \Theta(\log_2\, n)

    why it is allowed: Lines 10 and 12 bound WW by the same growth, log⁡2 n\log_2\, n.

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

this tests where the formula comes from, on the learn page

what a full answer includes
  • The parts 20, 10, 5, 2 and 1, and 5 looks.
  • The formula giving 4 and 1 more, 5.
  • The parts 20, 9, 4 and 1, 4 looks, and the first of two middles.