Question:

A be a sorted array containing 1000 distinct integers. You perform recursive binary search on A to find an element y. Suppose each comparison checks whether the middle element computed during the current recursive step is equal to, less than, or greater than y. The max number of comparisons that may have to be performed if y is not an element of A is __________ . (Answer in int)

Show Hint

The worst-case time complexity of binary search is O(log n). The number of comparisons is a direct application of this. Remember the formula $\lfloor \log_2 n \rfloor + 1$ for the maximum number of comparisons in a binary search implementation on an array of size n.
Updated On: Feb 23, 2026
Hide Solution
collegedunia
Verified By Collegedunia

Correct Answer: 10

Solution and Explanation

Step 1: Understanding the Question:
We need to find the maximum number of comparisons required for a binary search on a sorted array of 1000 elements, specifically in the worst-case scenario where the element is not present in the array.
Step 2: Key Formula or Approach:
Binary search works by repeatedly dividing the search interval in half. The number of comparisons required in the worst case is the number of times we can divide the array size 'n' by 2 until we are left with a subarray of size less than 1. This can be expressed logarithmically. For a search (successful or unsuccessful), the maximum number of comparisons is given by $\lfloor \log_2 n \rfloor + 1$.
Step 3: Detailed Explanation:
Let n = 1000. We need to calculate $\lfloor \log_2 1000 \rfloor + 1$.
We can find the value of $\log_2 1000$ by finding the power of 2 that is just less than or equal to 1000.
\[ 2^9 = 512 \] \[ 2^{10} = 1024 \] Since $2^9<1000<2^{10}$, the logarithm $\log_2 1000$ is between 9 and 10.
Therefore, the floor of the logarithm is: \[ \lfloor \log_2 1000 \rfloor = 9 \] Now, we add 1 to find the maximum number of comparisons: \[ \text{Max Comparisons} = 9 + 1 = 10 \] Alternatively, we can trace the size of the search space: 1. Start: 1000 elements
2. After 1 comp: $\approx$ 500
3. After 2 comps: $\approx$ 250
4. After 3 comps: $\approx$ 125
5. After 4 comps: $\approx$ 62
6. After 5 comps: $\approx$ 31
7. After 6 comps: $\approx$ 15
8. After 7 comps: $\approx$ 7
9. After 8 comps: $\approx$ 3
10. After 9 comps: $\approx$ 1
After 9 comparisons, the search space is reduced to a single element. A 10th comparison is needed to check this final element and conclude that `y` is not present (if `y` is not that element).
Step 4: Final Answer:
The maximum number of comparisons required is 10.
Was this answer helpful?
0
0

Questions Asked in GATE DA exam

View More Questions