Skip to content

Binary search

finding

Level 1 of 3

Also called Half-interval search and Logarithmic search.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from binary search's own run on the numbers shown.

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.

new numbers

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.

new numbers

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.

new numbers