Binary search looks for 46 in 5, 14, 20, 33, 39, 46, 53, 64, 71. At which place is its second look?
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.
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
A46 stands at place 6, but the second look lands at place 7, before 46 is reached.
CPlace 8 is the second of the two middles. This binary search takes the first.
DPlace 3 is in the half the first look ruled out, since 46 is bigger than 39.
2question 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?
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.
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
AThat is linear search's answer, and bisect_left's, which gives index 1. Binary search never looks at place 2 here.
CPlace 3 is never looked at, since the first look already finds 17.
DPlace 1 holds 9, which is not 17.
3question 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.
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
A13 is a middle of the part before 34, the second look.
B47 is on level 3, 3 looks.
D34 is the middle, found at the first look.
4question 4
Which is true of binary search's looks?
the answer, and why
the answer is C
The middle costs 1 look, . A number bigger than all costs ,.
AA ceiling is not a tight bound. The middle still costs 1 look.
BIn a row of 12, a number smaller than all of them costs 3 looks, one fewer than the most.
DDoubling the row adds one halving, so one look.
pen and paper
5question 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.
How many places stand on each level of its tree of looks?
Add the looks over every place, then average them.
Check your average against the formula, with 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 as 3, gives 4 times 16, which is 64, less 16, plus 1, which is 49, over 15, so 49 over 15 too.
level
places on it
looks each
looks in all
1
1
1
1
2
2
2
4
3
4
3
12
4
8
4
32
in all
15
49
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 looks on average, each place as likely?23 of 23 shown
1the move: price one place
the line:
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.
level
places on it
looks each
looks in all
1
1
1
1
2
2
2
4
3
4
3
12
in all
7
17
Added level by level, 17 looks over 7 places.
why it is allowed: Level holds the places a search reaches at its look number ; the middle is level 1. Here every number is different: copies would stop some searches sooner.
2the move: compare the parts
the line:
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.
level
parts
their sizes
places on it
1
1
10
1
2
2
4 and 5
2
3
4
1, 2, 2 and 2
4
4
8
0, 0, 0, 1, 0, 1, 0 and 1
3
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.
guess it, then
3the move: fill the levels
the line:
why it is allowed: On 10, 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 , 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 lands on it.
guess it, then
4the move: count level by level
the line:
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.
level
places above
places paying
1
0
0, none
10
2
1
1
9
3
2
1 + 2, which is 3
7
4
3
1 + 2 + 4, which is 7
3
in all
29
The 10 numbers again. 29 looks over 10 places is 2.9.
why it is allowed: is the average, every place's looks added, over . Each place pays a look on every level down to its own. Here counts the full levels above a level, not halvings. That level is paid by every place but the 1 + 2 + ... + above it. The sum stops at , so at as 1 it is just 1, and at as 0 nothing, as the grid's rows show.
guess it, then
5the move: use the doubling sum, j for n
the line:
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 . What is the total?
1the move: name the sum
the line:
why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
2the move: double both sides
the line:
why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
3the move: take line 1 away
the line:
why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
4the move: tidy the left side
the line:
why it is allowed: Two lots of less one lot is one lot.
5the move: cancel what both brackets share
the line:
when n is 3
1
2
4
8
16
first bracket
2
4
8
16
second bracket
1
2
4
8
left over
take away 1
16
Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.
why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
6the move: the terms before the last
the line:
why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
7the move: tidy the power
the line:
why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
8the move: read it
the line:
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 , twice the last.
why it is allowed: The box under this line shows a doubling sum from 1 is one less than its next term, here with for its . For 3 levels above, 1 + 2 + 4 is 7, one less than 8.
guess it, then
6the move: tidy each term
the line:
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.
guess it, then
7the move: split the sum
the line:
why it is allowed: Add up the s and the powers of 2 apart. For 10, 11 + 11 + 11 + 11 is 44; take away 1 + 2 + 4 + 8, which is 15: 29.
guess it, then
8the move: gather the terms
the line:
why it is allowed: runs from 0 to , so each sum has terms, which make lots of , less the powers of 2.
guess it, then
9the move: use the doubling sum, h for n
the line:
where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total?
1the move: name the sum
the line:
why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
2the move: double both sides
the line:
why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
3the move: take line 1 away
the line:
why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
4the move: tidy the left side
the line:
why it is allowed: Two lots of less one lot is one lot.
5the move: cancel what both brackets share
the line:
when n is 3
1
2
4
8
16
first bracket
2
4
8
16
second bracket
1
2
4
8
left over
take away 1
16
Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15.
why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
6the move: the terms before the last
the line:
why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
7the move: tidy the power
the line:
why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
8the move: read it
the line:
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 , twice the last.
why it is allowed: The same box, with for its , the doubling sum up to is less 1. For 10, is 3, and 1 + 2 + 4 + 8 is 15.
guess it, then
10the move: tidy
the line:
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.
guess it, then
11the move: a ceiling from the most looks
the line:
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.
guess it, then
12the move: drop the rounding
the line:
why it is allowed: By line 11, as rounding down never makes a number bigger.
guess it, then
13the move: a fixed multiple
the line:
why it is allowed: From 2 numbers on, 1 is at most .
guess it, then
14the move: read it as a bound
the line:
why it is allowed: By line 13, with the fixed multiple 2.
guess it, then
15the move: a floor
the line:
why it is allowed: From line 10. is at most , as is rounded down, so is at most , and taking away takes at least as much. For 10, 8 is at most 10, so 16 is at most 20.
guess it, then
16the move: multiply out
the line:
why it is allowed: times is lots of , and more. For 10, 44 less 20, plus 1, is 25, so 2.5, under the true 2.9.
guess it, then
17the move: gather
the line:
why it is allowed: lots of , less 2 lots, leave lots, and , plus 1, is . For 10, 25 over 10.
guess it, then
18the move: share out
the line:
why it is allowed: Share each part of line 17 by . lots of , over , is . For 10, 2 plus 5 over 10, which is 2.5.
guess it, then
19the move: drop the part over n
the line:
why it is allowed: over 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.
guess it, then
20the move: a third of h + 1
the line:
2
1
1
3
about 1.33
2
4
about 1.67
3
5
2
4
From as 2, a third of grows by a third each step, and by 1.
why it is allowed: The aim is a fixed multiple of under . A third, because at as 2 both sides are 1, and grows faster after, as the table shows. From 4 numbers on, is at least 2.
guess it, then
21the move: a simpler floor
the line:
why it is allowed: is less than . At 4, line 20 gives 1, this line two thirds, and is 2.
guess it, then
22the move: read it as a bound
the line:
why it is allowed: By line 21, with the fixed multiple a third.
guess it, then
23the move: ceiling and floor together
the line:
why it is allowed: Lines 14 and 22 bound by the same growth, , with the fixed multiples 2 and a third.
guess it, then
try it on small inputs: over the 7 places of a row of 7 different numbers, each tried once, the average is about 2.43
A row of 20 numbers in order. Binary search looks for a number bigger than all of them, then for one smaller than all.
Write how many numbers each call searches, for the bigger number, until none is left.
Count the looks, and check them against .
Do the same for the smaller number. Why is it one look fewer?
a hint
After its middle, a part of keeps . Before it stand one fewer when 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.
look
bigger, its part holds
smaller, its part holds
1
20
20
2
10
9
3
5
4
4
2
1
5
1
none left
The part each look searches. The bigger number needs 5 looks, the smaller 4.
Why does a number bigger than every number cost looks?13 of 13 shown
1the move: watch the search
the line:
Looking for 95: 4 looks, not there.
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.
2the move: bigger than all
the line:
call
its part holds
its middle
kept after it
looks so far
1
10
place 5
5
1
2
5
place 8
2
2
3
2
place 9
1
3
4
1
place 10
0
4
5
0
none, so no look
4
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 numbers takes the first of two middles, so half, rounded down, stand after it, . A number bigger than all keeps that side.
guess it, then
3the move: write the calls as a cost
the line:
why it is allowed: The looks on numbers are 1, plus the looks on . 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.
guess it, then
4the move: unroll it
the line:
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 counts the halvings so far, so call 1, with 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.
guess it, then
5the move: add up the calls
the line:
why it is allowed: is the looks for a number bigger than all, and the halvings, rounding down, that bring to 1. By line 4, the calls with from 0 to each look once, and the next holds none. adds the 1 after it once for each from 0 to . For 10, is 3.
guess it, then
6the move: use the page on halving: its count of halvings is our h
the line:
where does this come from?
question 3 of the page halving and logarithmsBinary search on a row of numbers. At most how many looks?
1the move: what one look keeps
the line:
8 numbers have two middles; the table's 7 have one.
look
still to search
half, rounded down
the middle
kept
1
10, 20, 30, 40, 50, 60, 70
3.5 to 3
40
50, 60, 70
2
50, 60, 70
1.5 to 1
60
70
3
70
0.5 to 0
70
none
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.
2the move: fewer numbers, no more looks
the line:
after look
keeping the bigger side
keeping the smaller side
0
10
10
1
5
4
2
2
1
3
1
none
4
none
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.
3the move: a number that keeps the bigger side
the line:
why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.
4the move: count that search's looks
the line:
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.
5the move: the most looks
the line:
why it is allowed: Let 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.
6the move: read it
the line:
why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.
why it is allowed: The box counts these halvings, keeping the bigger side, so is , and shows no search makes more looks.
guess it, then
7the move: count the calls, h for n
the line:
numbers
halved down to 1
calls that look
8
8, 4, 2, 1
4
3
10
10, 5, 2, 1
4
3
15
15, 7, 3, 1
4
3
16
16, 8, 4, 2, 1
5
4
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 items cost?
1the move: unroll it into levels
the line:
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.
level
copies
items in each
looks at this level
0
1
8
1
1
1
4
1
2
1
2
1
3
1
1
1
in all
4
The levels for 8 items, with as 3.
why it is allowed: Let be a level's number, 0 at the top. After halvings, twos are left, so level holds items. For 8, level 1 holds 4, which is 2 to the power 2. The levels stop at level , 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.
2the move: add up the levels
the line:
why it is allowed: Let be the cost, the looks of every level added. , read the sum of, adds the term after it, here 1, once for each , from 0 under the sign to over it. For 8, is 3, so it is 1 + 1 + 1 + 1.
3the move: count the levels
the line:
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 , which is of them, each adding 1. For 8, 4 looks.
4the move: read it
the line:
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.
why it is allowed: The calls with from 0 to are one more than , 1 look each. For 10, is 3, so is 1 + 1 + 1 + 1, which is 4. The box under this line counts the same shape on rows that halve exactly, its being this page's .
guess it, then
8the move: drop the rounding
the line:
why it is allowed: By line 7, as rounding down never makes a number bigger.
guess it, then
9the move: a fixed multiple
the line:
2
1
2
2
4
2
3
4
10
about 3.32
4
about 6.64
16
4
5
8
1,000
about 9.97
10
about 19.93
stands between and twice it, from 2 numbers on.
why it is allowed: From 2 numbers on, is at least 1, so the 1 of line 8 is at most .
guess it, then
10the move: read it as a bound
the line:
why it is allowed: By line 9, with the fixed multiple 2.
guess it, then
11the move: a floor
the line:
why it is allowed: By line 7. Rounding down takes off less than 1, and line 7 adds 1 back.
guess it, then
12the move: read it as a bound
the line:
why it is allowed: By line 11.
guess it, then
13the move: ceiling and floor together
the line:
why it is allowed: Lines 10 and 12 bound by the same growth, .
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand