Skip to main contentSkip to solution

Arrange the following steps of the binary search algorithm in the correct order.

(A) Compare the target element to the middle element of the array.

(B) If equal, return the index.

(C) If the target element is less than the middle element, then repeat search in the left subarray, else if the target element is greater than the middle element, then repeat search in the right subarray.

(D) If subarray is empty, conclude that the target element is not present in the array.

Solution

✅ Correct Option: 1

Binary search first compares the target with the middle element, returns the index if they match, and otherwise narrows the search to one half. If the narrowing ends with an empty subarray, the target is absent.

(A) Compare the target element to the middle element of the array.

(B) If equal, return the index.

(C) If not equal, repeat the search in the left subarray (target is smaller) or the right subarray (target is greater).

(D) If the subarray becomes empty, conclude that the target is not present.


Option 2: (C) applies only when the element is not equal to the middle, so the equality check (B) must come before narrowing.

Options 3 and 4: (D) cannot come before (C), because the subarray becomes empty only after repeated narrowing.

Keyboard Shortcuts

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