OOPS
April 19, 2024
Arrays
April 19, 2024
OOPS
April 19, 2024
Arrays
April 19, 2024

Question 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

A
2
B
15
C
17
D
1

Leave a Reply

Your email address will not be published. Required fields are marked *