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 44 in 1, 15, 21, 40, 43, 44, 47, 76, 86. At which place is its second look?

11521404344477686
the answer, and why

the answer is C

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

if you chose another

  • 44 stands at place 6, but the second look lands at place 7, before 44 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 44 is bigger than 43.

new numbersthe test page's numbers

question 2

Linear search would stop at the first 83, place 2. At which place does binary search stop, looking for 83 in 3, 83, 83, 83, 84, 91, 95?

3838383849195
the answer, and why

the answer is D

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

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 83.
  • Place 1 holds 3, which is not 83.

new numbersthe test page's numbers

question 3

Which search costs binary search the most looks?

the answer, and why

the answer is A

Binary search takes 4 looks for 1, 22, 23, 31, 33, 40, 58, 76, 77, 83; 83, the most of the four.

if you chose another

  • Binary search takes 3 looks for this one, fewer than 4.
  • Binary search takes 2 looks for this one, fewer than 4.
  • Binary search takes 3 looks for this one, fewer than 4.

new numbersthe test page's numbers