Data Structures & AlgorithmsModerate
Binary search with mid = (lo + hi) // 2 on the sorted array 2 5 8 12 16 23 38 56 72 91 110 130 looks for the key 91. How many probes (comparisons of a[mid] with the key) are made, including the final one that finds it?
- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
Each probe halves the live range; the count includes the probe that finds the key, and the midpoint is the floor of the average of the bounds. Forgetting the final probe, or searching for a different key, yields the other numbers.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Standard binary search (lo = 0, hi = n−1, mid = (lo+hi)//2) runs on 3 7 11 15 19 23 27 31 35 to look for the absent key 20. How many…
- Insertion sort (ascending) is run on 6 3 8 1 5. Each time the inner loop compares the key with an element to its left, that is one…
- A top-down merge sort splits a range of size n into floor(n/2) and ceiling(n/2) parts and stops at size 1 or 0. Counting the root call…
- Records are (name, key): (ram, 2), (sita, 1), (gita, 2), (mohan, 0), (ravi, 1), (asha, 2). A STABLE counting sort orders them by key 0, 1…
- Jump search uses block size m = 4 on the sorted array 1 4 6 9 11 14 18 21 25 30 33 37 40 44 48 51. It jumps by 4 while the last element of…
- The sorted array 2 5 8 11 13 17 21 was rotated so that it now reads 13 17 21 2 5 8 11. At which 0-based index is the value 11?