Consider the list of numbers 1, 2, 3, …, 1000 stored in a[0..999]. What will…
2017
Consider the list of numbers 1, 2, 3, …, 1000 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;
}
- A.
2
- B.
15
- C.
17
- D.
1
Attempted by 148 students.
Show answer & explanation
Correct answer: C
