A sorted list holds 12 values at positions 0 through 11: 4, 9, 13, 18, 22, 27, 31, 36, 40, 45, 51 and 58. You run a standard binary search for the value 33. It starts with low = 0 and high = 11, uses mid = (low + high) // 2 with integer division rounding down, sets low = mid + 1 when the probed value is smaller than 33, and sets high = mid − 1 when it is larger. The loop runs while low is less than or equal to high.
Give each answer as a whole number: the number of probes made before the search stops, the final value of low, the final value of high, and the value currently stored at the position given by the final low.
Unlock full course notes by signing up
Trace table for a binary search
An ascending list holds the values 6, 11, 18, 24, 30, 37, 45, 51, 59, 64, 72 at indices 0 through 10. A binary search looks for the value 24, computing mid as the floor of (low + high)/2 and moving low to mid + 1 or high to mid - 1 after each comparison. Complete the table with the values of low and high at the start of each iteration, the mid index, and the value stored at mid. The search finds the target in the third iteration.
Worst-case probes for 2048 records
A sorted array holds 2048 student records, ordered by student number. Binary search compares the target with one middle element per probe and discards the half that cannot contain it. What is the maximum number of probes needed to find a student number or confirm it is absent? Answer as a whole number.
Updating the interval after a probe
A binary search on a sorted list of integers uses mid = (low + high) // 2, where // is integer division that rounds down. Partway through a search, the interval is low = 10 and high = 25.
The next probe position is mid = ________. The element stored there is greater than the target, so the search keeps only positions below it and sets high = ________. The probe after that is at position ________. If the element at position 13 is smaller than the target, the search sets low = ________.
Evaluating claims about binary search efficiency
A programming team is deciding whether to use binary search or sequential search in several parts of an application. Which of the following statements about the efficiency of binary search are true? Select all that apply.