Skip to main contentSkip to solution

What represents the first three steps of BinarySearch(numlist,key) algorithm?

Solution

✅ Correct Option: 1

Indexing in a list starts at 0, so the search range begins with first=0first = 0 and ends with last=n−1last = n-1. The middle position is then mid=(first+last)/2mid = (first + last)/2.

The loop runs only while the search range is valid, that is, while first≤lastfirst \le last. Once firstfirst crosses lastlast, the range is empty and the key is not present.


Options 2 and 4: setting first=1first = 1 skips index 0, so the first element is never searched.

Options 3 and 4: the condition first≥lastfirst \ge last is wrong. At the start first<lastfirst < last, so the loop would not run at all (for n>1n > 1).

Keyboard Shortcuts

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