Comparisons and Searching in Algorithms: Worked Examples, Bounds and GATE Patterns
Count the operation a question actually asks for. These worked traces show how to analyse linear search, binary search, paired min-max and comparison lower bounds.
KnowledgeGate Team
Exam prep & CS education

Binary search is fast, but candidates lose marks by conflating loop iterations, key comparisons and boundary checks. A key comparison is distinct from loop tests, index updates and array accesses; linear and binary search, paired min-max, and comparison lower bounds use this distinction.
Comparisons and searching: define what the question is counting
The problem is to locate a key x in A[0..n-1]. A key comparison checks A[i] against x; loop tests, index updates and array accesses are different operations.
Here, one probe of A[i] against x counts as one key comparison, even if code writes equality and ordering separately. Supplied pseudocode asking for every relational evaluation overrides this convention.
Use Algorithm Analysis Basics: Operation Counts, Growth Rates and Worked Examples for general operation-count ledgers, asymptotic classes and loop machinery. Search-count questions instead isolate key comparisons in linear search, binary search, paired min-max and decision-tree bounds.
Linear search works on an unsorted array. Binary search needs monotonic sorted order and random access. A binary search tree makes similar decisions through pointers but may become unbalanced. These topics belong to GATE CS exam preparation.
Before calculating, identify whether the search succeeds, which case is requested, and whether preprocessing such as sorting is already paid for.
Linear search comparisons: calculate them directly
Take the unsorted array U = [14, 3, 27, 9, 18, 5, 21, 11].
For x = 18, scan from the left. The probes are 14, 3, 27, 9, 18. The fifth probe matches, so the result is index 4 after 5 key comparisons.
For x = 20, no value matches. All eight positions must be inspected, so the result is not found after 8 comparisons.
case | target | probed values | comparisons | result |
|---|---|---|---|---|
successful |
|
|
| index |
unsuccessful |
|
|
| not found |
The best case is 1 comparison and the worst is n. If a successful key is equally likely in any of the n positions, the expected count is
(1 + 2 + ... + n) / n = [n(n + 1)/2] / n = (n + 1)/2.
For n = 8, this is (8 + 1)/2 = 9/2 = 4.5. This average changes if early positions are more likely or unsuccessful searches are included.
Binary search comparisons: work through the shrinking interval
Now use S = [3, 5, 9, 11, 14, 18, 21, 27], with indices 0..7, inclusive low and high, and mid = floor((low + high)/2).
For x = 27:
(low, high, mid, S[mid]) = (0, 7, 3, 11). Since27 > 11, setlow = 4.(4, 7, 5, 18). Setlow = 6.(6, 7, 6, 21). Setlow = 7.(7, 7, 7, 27). Found at index7.
That is 4 key comparisons, versus 8 for left-to-right linear search.
For unsuccessful search x = 20, probe 11 at index 3, 18 at index 5, then 21 at index 6. Set high = 5 while low = 6. The empty interval stops the search after 3 comparisons.
The recurrence T(n) = T(floor(n/2)) + 1 gives worst-case successful count floor(log2 n) + 1. For n = 8, this is 3 + 1 = 4.
A balanced tree also narrows its search path, while a skewed tree can fall back to linear height. Tree-based search follows pointers and can become unbalanced.

Minimum and maximum together: save comparisons by pairing
For eight values, two independent scans cost 2(n - 1) = 2(7) = 14 comparisons: seven for the minimum and seven for the maximum.
Pair the values in U as (14, 3), (27, 9), (18, 5) and (21, 11). The four pair comparisons produce larger candidates [14, 27, 18, 21] and smaller candidates [3, 9, 5, 11]. Finding 27 among the four larger values costs 3 more comparisons. Finding 3 among the four smaller values costs another 3.
The total is 4 + 3 + 3 = 10, saving four comparisons. For even n, the count is
n/2 + (n/2 - 1) + (n/2 - 1) = 3n/2 - 2.
Odd n needs initialisation, so do not apply this formula blindly.

Comparison lower bounds: know when no clever algorithm can do better
In the decision-tree model for sorting distinct keys, each comparison creates a binary branch. Since all n! input orders need distinct leaves, the worst-case height is at least ceil(log2(n!)).
For five keys, 5! = 120. Six comparisons distinguish at most 2^6 = 64 leaves, which is insufficient. Since 2^7 = 128, every comparison sort needs at least 7 comparisons in its worst case.
This gives the asymptotic lower bound Omega(n log n), but excludes non-comparison methods such as counting sort. It describes the best possible comparison sort, not every implementation's exact count. Use Sorting Algorithms: Complexity and Comparison to compare methods.
Comparisons and searching traps: unsorted input, counting models, boundaries and preprocessing
Unsorted input: Apply binary-search decisions to the original U for x = 18. The probes are 9 at index 3, 5 at index 5, and 21 at index 6. Now low = 6 and high = 5, so the search wrongly reports not found even though 18 is at index 4. Sort first or use linear search.
Counting model: Four probes for x = 27 do not automatically mean four total relational evaluations in a particular program. Read whether the question counts key comparisons, all conditions or loop iterations, then annotate the pseudocode line by line.
Boundaries and duplicates: With inclusive bounds, continue while low <= high. A one-element interval still needs a probe. Ordinary binary search may return any duplicate occurrence. To find the first or last, save a match and continue on the required side.
Preprocessing: Consider ten searches over 16 fixed, distinct values. Standard merge sort needs at most 16 log2 16 - 16 + 1 = 64 - 16 + 1 = 49 comparisons. Each binary search needs at most floor(log2 16) + 1 = 5, giving 49 + 10(5) = 99. Ten independent linear searches can need 10(16) = 160. For one query, frequent updates or likely early matches, sorting may not repay its cost.
GATE comparison and searching patterns: recognise the requested quantity
Typical tasks ask you to trace pseudocode, choose the correct case, count minimum-plus-maximum comparisons, infer a decision-tree bound, or spot a broken binary-search precondition. More than 100 practice questions focus on Comparisons & Searching, so identify the requested quantity before calculating.
Common comparison and searching counts:
Binary search among
32sorted distinct keys:floor(log2 32) + 1 = 5 + 1 = 6successful key comparisons at most.Minimum of
10unsorted values:10 - 1 = 9comparisons.Paired min-max for even
n = 10:3(10)/2 - 2 = 15 - 2 = 13comparisons.Sorting
8distinct keys:ceil(log2(8!)) = ceil(log2 40320) = 16as the comparison lower bound.
Once you can justify each count, use the GATE Test Series to practise timed traces.
Comparisons and searching: key results and practice
Unsorted data plus one search means start with linear search.
Sorted data permits binary search.
Count exactly the operation named.
Pair candidates to reduce min-max work.
Use decision trees to prove comparison lower bounds.
Linear search needs 5 comparisons to find 18 and 8 to miss 20; binary search needs 4 to find 27 in the sorted eight-value array; paired min-max needs 10 comparisons on eight values.
For a structured route through algorithms and the syllabus, continue with GATE Guidance by Sanchit Sir. If this is your only weak area, redo the three traces unseen with different targets.
Keep learning
Comparisons and Searching MCQs: 12 Solved GATE, ISRO and PYQ Questions with Explanations
Solve 12 searching and comparison questions with complete traces, formulas and decision-tree reasoning. Each question includes its answer and a link to the solved page.
String Matching Algorithms Explained: Naive Search, KMP and Rabin-Karp
Learn how naive search, KMP, Rabin-Karp and related methods differ. Build an LPS array, trace KMP through mismatches and verify a rolling-hash collision.
C++ STL Algorithms: Sort, Search, Transform and Clean Data in One Pipeline
Trace nine sensor readings through sort, lower_bound, transform, erase-remove and accumulate, with every iterator rule and intermediate value explained.
Growth Rate Comparisons MCQs: 12 Solved GATE and NET Questions with Explanations
Twelve solved GATE and UGC NET growth-rate questions, each with its full option set, the log-trick comparison behind the answer, and a link into the PYQ pages.





