GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Binary search (lo = 0, hi = n - 1, mid = (lo + hi) / 2 with integer division) is run on a sorted array of the 100 elements 1..100 once for each key from 1 to 100. For how many keys does the search need the maximum number of loop iterations, and what is that maximum?

  1. A. 37 keys need 7 iterations
  2. B. 43 keys need 7
  3. C. 37 keys need 8
  4. D. 28 keys need 6

Correct answer: A. 37 keys need 7 iterations

A decision tree of 100 nodes has 7 levels; the last level holds 100 - (2^6 - 1) = 37 nodes.

More Programming and Data Structures (GATE) questions

Exams that ask this topic