Given a sorted array arr = {5,6,77,88,99} and key = 88; using binary search…

2024

Given a sorted array arr = {5,6,77,88,99} and key = 88; using binary search with mid = ⌊(low + high) / 2⌋, how many iterations are done until the element is found?

Answer: D. 2Concept: Binary search works only on a sorted array. At each iteration it computes the middle index of the current search range, mid = ⌊(low + high) / 2⌋, and…

  1. A.

    1

  2. B.

    3

  3. C.

    4

  4. D.

    2

Attempted by 72 students.

Show answer & explanation

Correct answer: D

Concept: Binary search works only on a sorted array. At each iteration it computes the middle index of the current search range, mid = ⌊(low + high) / 2⌋, and compares the key to the value stored there: if they match, the search stops; if the key is larger, the left half together with that midpoint is discarded and the search continues on the right portion; if the key is smaller, the right half together with that midpoint is discarded and the search continues on the left portion. Each such middle-element comparison counts as one iteration.

The stem names binary search and fixes the midpoint rule as mid = ⌊(low + high) / 2⌋, and the array is already in ascending order, so what is counted here is the number of midpoint comparisons binary search performs — a different quantity from the number of positions a sequential left-to-right scan would visit on the same array.

Application: Applying this to arr = [5, 6, 77, 88, 99] (0-indexed) with key = 88:

  1. Iteration 1 — the range is index 0 to 4, so mid = ⌊(0 + 4) / 2⌋ = 2, and arr[2] = 77. Since 77 is less than 88, indices 0 to 2 — the left half together with the midpoint — are discarded and the search moves to indices 3 to 4.

  2. Iteration 2 — the range is now index 3 to 4, so mid = ⌊(3 + 4) / 2⌋ = 3, and arr[3] = 88. This matches the key exactly, so the search stops here.

Cross-check: Binary search on a sorted array behaves like descending an implicit balanced search tree built from the array's midpoints — the first midpoint (index 2) is the tree's root, and the midpoints of each half are its children. Index 3 sits one level below the root in that tree (a child of the {3, 4} half), so reaching it takes exactly one more step after the root — two steps in total, independently confirming the count from the direct trace above.

So the key 88 is found after 2 iterations.

Explore the full course: Coding For Placement

Loading lesson…