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…
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;
}
Show answerHide answer
Correct answer: (C) 17