Skip to main contentSkip to solution

Consider a list [-4, 0, 2, 7, 8, 17, 19].

Apply binary search to find element -4. Determine the number of key comparisons required.

Solution

✅ Correct Option: 3

Binary search compares the key with the middle element and discards half the list each time. Here the list has 7 elements at indices 0 to 6, and mid=⌊(low+high)/2⌋mid = \lfloor (low + high)/2 \rfloor.

ComparisonlowhighmidElement at midOutcome
10637−4<7-4 < 7, search left half
20210−4<0-4 < 0, search left half
3000-4Found

Key comparisons =3= \boxed{3}

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question