IAE Academy

GATE 2024 DA Q.40: solution and answer

GATE 2024 · DAQ.40 · 2 marks · MCQ

Let denote the maximum number of comparisons made while searching for an entry in a sorted array of size using binary search.

Which ONE of the following options is TRUE?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

See the question in the paper

Answer and solution

ANSWER

The correct option is (A): .

Official answer: ASource: Official answer key, GATE 2024 DA (IISc Bengaluru)

  1. Binary search compares the key with the middle element once. If it is not a match, the search continues in exactly one of the two remaining parts, never both.

  2. With the middle element removed from an array of size , the two parts have sizes and . The larger part therefore has exactly elements. The worst case is when the key lies in the larger part (or is absent).

  3. So the worst case is one comparison plus the worst case on elements: with . This solves to . Values: , , , , .

  4. Check (B): would add the costs of both halves, as an algorithm does when it processes both halves. It gives , but , whereas the true value is . So (B) is false.

  5. Check (C): ignores the comparison at the middle. At it says , but binary search on two elements can need 2 comparisons. So (C) is false.

  6. Check (D): would give , but the true value is . It describes linear search, not binary search. So (D) is false.

  7. Only (A) is true.

KEY IDEA

Binary search makes one comparison at the middle, then continues in only one part, and the larger part has elements. The worst case therefore satisfies , which gives comparisons.

WATCH OUT FOR

Choosing (B) by copying the merge sort recurrence, which adds the costs of both halves, or choosing (C) by forgetting that the middle comparison costs one step each time.

From the GATE 2024 DA paper. Every question of this paper · Download the full paper · All 18 Sorting & hashing questions, with notes

More Sorting & hashing questions