• edited by
30,090 views
61 61 votes

Consider the following C program that attempts to locate an element $x$ in an array $Y[ \ ]$ using binary search. The program is erroneous. 

 f (int Y[10] , int x) {
    int i, j, k;
    i= 0; j = 9;
    do {
        k = (i+ j) / 2;
        if( Y[k] < x) i = k;else j = k;
        } while (Y[k] != x) && (i < j)) ;
    if(Y[k] == x) printf(" x is in the array ") ;
    else printf(" x is not in the array ") ;
 }

On which of the following contents of $Y$ and $x$ does the program fail? 

  1. $Y$ is $[1 \ 2 \ 3 \ 4 \ 5 \ 6 \ 7 \ 8 \ 9 \ 10]$ and $x < 10 $  
  2. $Y$ is $[1 \ 3 \ 5 \ 7 \ 9 \ 11   \ 13 \ 15 \ 17 \ 19]$ and $x < 1 $ 
  3. $Y$ is $[2 \  2 \ 2 \ 2 \  2 \  2 \  2 \ 2 \ 2 \ 2]$ and $x > 2$ 
  4. $Y$ is $[2 \ 4 \ 6 \ 8 \ 10 \ 12 \ 14 \ 16 \ 18 \ 20]$ and $ 2 < x < 20$ and $x$ is even

5 Answers

Best answer
55 55 votes

when it is option C the control will continue to iterate as $i=8$ and $j=9$;
again and again $i$ will be assigned $k$ which itself equals $8$ as $\frac{8+9}{2}$ being stored in an integer type variable, will evaluate to $8$.


For option A, with $x=9, k$ will take the following values:

  • $4$
  • $6$
  • $7$
  • $8 - y[8] = 9, x$ found

For option D, with $x=10, k$ will take the following values:

  • $4, y[4] = 10, x$ found
• edited by
16 16 votes
 do {
        k = (i+ j) / 2;
        if( Y[k] < x) i = k;else j = k;
    } while (Y[k] != x) && (i < j)) ;

Here i=k and j=k creates the problem.

just do i=k+1 and j=k-1

when we search the elements which is not in array and that element in the range of first and last element  results infinite looping. that is unsuccessful search and range should be inbetween first and last.

another problem when searching last element and greater than of last element.

• edited by
8 8 votes

Above code goes into infinite loop if element to be found is greater or equal to last element.

in option (C) we are searching for x>2 so it will go into infinite loop

Answer option (C) 

5 5 votes
For binary search there are two necessary conditions:

1.Array should be sorted.

2.All the elements in the array should be distinct.

for Ques.84 option c is correct
1 flag:
✌ Edit necessary (oogway69 “wrong reasoning”)
4 4 votes
84. The answer is C.

Binary search fails if all the elements are same.

85. The answer is A.
1 flag:
✌ Edit necessary (oogway69 “wrong reasoning”)
Answer:
Position:
Show:

Related questions

30 30 votes
3 answers 3 answers
11.9k
11.9k views
go_editor asked Apr 23, 2016
11,873 views
Consider the following C program that attempts to locate an element $x$ in an array $Y[ \ ]$ using binary search. The program is erroneous. f (int Y[10] , int x) { int i,...
74 74 votes
4 answers 4 answers
36.1k
36.1k views
Kathleen asked Sep 12, 2014
36,075 views
Which of the following are NOT true in a pipelined processor?Bypassing can handle all RAW hazardsRegister renaming can eliminate all register carried WAR hazardsControl h...
43 43 votes
6 answers 6 answers
22.1k
22.1k views
go_editor asked Apr 23, 2016
22,101 views
Consider the following C functions:int f1 (int n) { if(n == 0 || n == 1) return n; else return (2 * f1(n-1) + 3 * f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N];...
37 37 votes
6 answers 6 answers
11.1k
11.1k views
go_editor asked Apr 23, 2016
11,064 views
Let $x_n$ denote the number of binary strings of length $n$ that contain no consecutive $0$s.The value of $x_5$ is $5$$7$$8$$16$