Data Structures & AlgorithmsModerate

Binary search with mid = (lo + hi) // 2 on the sorted array 2 5 8 12 16 23 38 56 72 91 110 130 looks for the key 91. How many probes (comparisons of a[mid] with the key) are made, including the final one that finds it?

  1. A. 4
  2. B. 3
  3. C. 5
  4. D. 6

Correct answer: A. 4

Each probe halves the live range; the count includes the probe that finds the key, and the midpoint is the floor of the average of the bounds. Forgetting the final probe, or searching for a different key, yields the other numbers.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic