The Gateway to Computer Science Excellence
+36 votes

Consider the C function given below. Assume that the array $listA$ contains $n (>0)$ elements, sorted in ascending order.

int ProcessArray(int *listA, int x, int n)   
        int i, j, k;     
        i = 0;     j = n-1;     
         do { 
               k = (i+j)/2;         
               if (x <= listA[k]) j = k-1;         
               if (listA[k] <= x) i = k+1; 
        while (i <= j);     
        if (listA[k] == x) return(k);     
        else   return -1;   

Which one of the following statements about the function $ProcessArray$ is CORRECT?

  1. It will run into an infinite loop when $x$ is not in $listA$.
  2. It is an implementation of binary search.
  3. It will always find the maximum element in $listA$.
  4. It will return −$1$ even when $x$ is present in $listA$.
in DS by Veteran (106k points)
edited by | 4.3k views
Why is ProcessArray called again,if it is a BINARY SEARCH?
If someone believes that option (A) is also correct, then following is the counter example:

listA={10, 20, 30, 40, 50}

x = 60;

k = (0+4)/2 = 2

listA[2] <= 60; hence i=k+1 = 3

k = (3+4)/2 = 3

listA[3]<= 60; Hence i = k+1 = 4;

k = (4+4)/2 = 4

listA[4] <= 60, hence i=k+1 = 5

Now, i>j, so loop terminates and returns -1.
It seems like option D is also correct


How you are getting that. Please provide an example.

8 Answers

+19 votes
Best answer
This is an implementation of the Binary search algorithm.

Note that the loop will be terminated when we have found $x$. In that case both the if conditions will be true making condition inside the while as false i.e., $i>j$.

Correct Answer: $B$
by Loyal (9.5k points)
edited by
Option B is correct because if element not present in the array the loop will not run infinite time.
e.g {10, 20, 30, 40, 50} and search 60 on this array.

x = 60;
k = (0+4)/2 = 2

listA[2] <= 60; hence i=k+1 = 3

k = (3+4)/2 = 3

listA[3]<= 60; Hence i = k+1 = 4;

k = (4+4)/2 = 4

listA[4] <= 60, hence i=k+1 = 5

after this loop will terminate. B part is correct
+16 votes
by (129 points)

when x == A[k], both "if" conditions will be true and this where it gets out of loop, nice question :)

Thanks for the hint, :) I was wondering how it was coming out of the loop.
nice explanation would you mind expanding the answer a bit, it is extremely useful (pun intended)
+14 votes

The trick is using <= in two if conditions.

if (x <= listA[k]) j = k-1;         
if (listA[k] <= x) i = k+1; 

So, at any time if A[k]=x then , both the if conditions will be executed, hence j=k-1 and i=k+1 will be set  => which will make sure that i>j( since (k+1)>(k-1)) and execution will come out of do-while loop.Answer B

by Active (3k points)
+4 votes
above code is implementation of binary search
by Boss (31.5k points)
Can you explain how ???
Could you pease explain, how option A is wrong. For array A={1, 2, 3, 4, 5} and K=10, then im getting  infinite iterations... Please help
Assume array contains 1,2,3,4. Will the above code give us correct answer? Please guide.
you are right ..even i have same doubt.and that is correct also(i feel).can any one explain why A option wrong

@Yashaswini :- Can you give any example where A is true?

+2 votes
The function is iterative implementation of Binary Search using Do while loop.
by Active (4.7k points)
+1 vote
Suppose we have 6 elements in array indexed from 0 to 5 with same values in it.

we have to search for 4.

i=0 j=4 then k =2


i=3 j=4 k=3


i=4 j=4 k=4 (we got the required elements in k)


i=5 j=3  now while condition fails and loop terminates

now x==A(k)=4

so binary search succeded
by Active (2k points)
+1 vote
The function is iterative implementation of Binary Search.  

k keeps track of current middle element.

i and j keep track of left and right corners
of current subarray
by Boss (10k points)
0 votes
When listA[k]=x;
then due to if(x<=listA[k]), j will be k-1
and due to if(listA[k]<=x), i will be k+1
thus, i>j because for every positive value of k, (k+1)>(k-1). So the loop will be terminated.
So it is a perfect working of Binary Search.
by (11 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,833 questions
57,688 answers
107,246 users