Curated · Linear Search & Binary Search

NAT · 1 mark · Problem PROG-SRCH-0002

Binary search is run on a sorted array of \(8\) distinct elements. Each comparison checks whether the middle element of the current range is equal to, less than, or greater than the key. The maximum number of comparisons that may be needed is .

Numerical answer (NAT).

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionEach comparison with the middle element either finds the key or discards about half of the remaining range. In the worst case the range shrinks \(8\to4\to2\to1\), and the last remaining element still has to be compared:\[\boxed{C_{\max}(n)=\lfloor\log_2 n\rfloor+1}\qquad C_{\max}(8)=3+1=4 .\]The common slip is \(\log_2 8=3\). That counts the halvings but forgets the comparison with the final element.Answer: 4

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com