What is the condition for binary search to be unsuccessful where Beg is the…

2021

What is the condition for binary search to be unsuccessful where Beg is the beginning and End is ending location of sorted array?

Answer: C. End < BegIterative binary search maintains two pointers, Beg and End, that bracket the region of the sorted array still under consideration. The loop invariant is: the…

  1. A.

    End > Beg

  2. B.

    End == Beg

  3. C.

    End < Beg

  4. D.

    End != Beg

Attempted by 452 students.

Show answer & explanation

Correct answer: C

Iterative binary search maintains two pointers, Beg and End, that bracket the region of the sorted array still under consideration. The loop invariant is: the search continues only while the bracket is non-empty, i.e. while Beg <= End. The moment this invariant breaks — the moment End becomes strictly less than Beg — there is no index left inside the bracket, so the algorithm has nothing left to examine and reports an unsuccessful search.

  1. Start with Beg = 0 and End = n − 1 for an n-element sorted array, and compute mid = (Beg + End) / 2 at each step.

  2. If the target equals a[mid], the search succeeds immediately.

  3. If the target is larger than a[mid], move the bracket right by setting Beg = mid + 1; if it is smaller, move the bracket left by setting End = mid − 1.

  4. Each iteration narrows the bracket, so the width End − Beg strictly decreases; if the target is absent, an update eventually pushes End below Beg.

  5. Once End < Beg, the bracket [Beg, End] contains no valid index, so the loop condition Beg <= End becomes false and the search terminates as unsuccessful.

Cross-check with a small trace: for a 5-element array (indices 0–4) searching for a value that is not present, the bracket can shrink from [0, 4] down to a step where End = 2 and Beg = 3 after the last comparison — here End is less than Beg, exactly the End < Beg condition, and the loop exits. No other relation between Beg and End (End > Beg, End == Beg, or End != Beg alone) forces the bracket to be empty, so End < Beg is the unique condition under which the loop guard Beg <= End fails.

The condition for an unsuccessful binary search is: End < Beg.

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…