Data Structures :: Searching & Sorting - Discussion
Home > Data Structures > Searching & Sorting > MCQs Questions Discussion
4 / 69
Which one of the following statements about the function Process Array is CORRECT?
Consider the C function given below. Assume the array listA contains (n>0) elements, sorted in ascending order.
int Process array (int * list A, int x, int n)
{
int i, j, k;
i =0;j=n-1;
do {
k = (i+j)/2;
if (x<=list A[k])
j=k-1;
if (list A[k] <=x)
i =k+1;
} while (i <=j);
if (list A[k] == x)
return (k);
else
return -1;
}
Consider the C function given below. Assume the array listA contains (n>0) elements, sorted in ascending order.
int Process array (int * list A, int x, int n)
{
int i, j, k;
i =0;j=n-1;
do {
k = (i+j)/2;
if (x<=list A[k])
j=k-1;
if (list A[k] <=x)
i =k+1;
} while (i <=j);
if (list A[k] == x)
return (k);
else
return -1;
}
AIt will run into an infinite loop when x is not in listA
BIt is an implementation of binary search.
CIt will always find the maximum element in listA.
DIt will return -1 even when x is present in listA.
Answer: Option (Login/Signup)
Show Explanation
Asked In ::
No Discussion on this question yet!