GATE Computer ScienceModerateGATE CS (PYQ pattern)
Binary search on a sorted array of 20 distinct keys uses mid = ⌊(lo + hi)/2⌋ and one three-way comparison per probe, stopping when lo > hi. For an absent key, each of the 21 gaps (before the first key, between consecutive keys, after the last) is equally likely. What is the average number of probes in an unsuccessful search?
- A. 3.70
- B. 5.00
- C. 4.48
- D. 4.33
Correct answer: C. 4.48
The search tree of 20 keys has levels 1–4 full (15 nodes) and 5 nodes on level 5, so 21 external gaps: the 10 below level-5 nodes cost 5 probes, the other 11 cost 4. Average = (10·5 + 11·4)/21 = 94/21 ≈ 4.48. 3.70 is the successful-search average; 5.00 assumes every failure reaches the bottom level.
More Algorithms questions
- A base-3 (ternary) counter starts at 0 and is incremented 1000 times. The cost of an increment is the number of digits that change…
- A dynamic array starts with capacity 2. When it is full and an element is appended, the capacity becomes ⌈1.5 × capacity⌉ and all current…
- Consider t(n): if n ≤ 2 return 1; else return t(n−1) + t(n−2) + t(n−3). How many calls to t (including the initial call) are made when…
- How many times is x++ executed? for (i = 2; i <= 100; i++) for (j = ii; j <= 100; j += i) x++;
- How many times is x++ executed? for (i = 1; i <= 1000; i = 2) for (j = i; j >= 1; j /= 3) x++; (integer division)
- Let f(n) = (log₂ n)^(log₂ n), g(n) = n^(log₂ log₂ n) and h(n) = (log₂ log₂ n)!. Which of the following is correct? I. f(n) = Θ(g(n)). II…