OOPS
April 19, 2024Arrays
April 19, 2024Question 8476 – Arrays
Consider the list of numbers 1, 2, 3, …, 1000 is stored in a[0..999]. What will be the total number of comparisons to search x = 501 using the following binary_search() function?
int binary_search(int a[ ], int n, int x)
{
int low=0, high=n-1;
while(low <= high)
{
int m = (low + high) /2:
if(x > a[m])
low = m+1;
else if(x < a[m])
high= m-1:
else
return m;
}
return -1;
}
Correct Answer: C
2
15
17
1
