GATE | GATE CS 1996 | Question 66

Consider the following program that attempts to locate an element x in an array a[ ] using binary search. Assume N>1. The program is erroneous. Under what conditions does the program fail?

var i,j,k: integer;  x: integer;
    a: array; [1....N] of integer;
begin    i:= 1; j:= N;
    k:(i+j) div 2;
    if a[k] < x then i:= k 
    else j:= k 
until (a[k] = x) or (i >= j);
if (a[k] = x) then
    writeln ('x is in the array')
    writeln ('x is not in the array')



Quiz of this Question
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.