12-01-2007, 06:21 PM
program Binary_search;
uses crt;
var x,y,n,no, temp, ub,lb,mid :integer;
a: array[1..10] of integer;
procedure enter;
begin
for x:=1 to n do begin
write('no. ',x,' makr= ');
readln(a[x]);
end
end;
procedure sort;
begin
for x:=1 to n-1 do
for y:=1 to n-x do
if a[y] >= a[y+1] then begin
temp:=a[y]; a[y]:=a[y+1]; a[y+1]:=temp;
end;
end;
procedure lsearch;
begin
ub:=1; lb:=n;
repeat
mid:=(ub+lb) div 2;
if no>a[mid] then ub:=mid else
if no<a[mid] then lb:=mid else
if no=a[mid] then write('Found ')
until no=a[mid];
writeln('at the position ',mid)
end;
Begin
randomize;
clrscr;
n:=10;
enter;
sort;
for x:=1 to n do writeln(x,a[x]:4);
write('Enter the mark to search ='); readln(no);
lsearch;
End.
uses crt;
var x,y,n,no, temp, ub,lb,mid :integer;
a: array[1..10] of integer;
procedure enter;
begin
for x:=1 to n do begin
write('no. ',x,' makr= ');
readln(a[x]);
end
end;
procedure sort;
begin
for x:=1 to n-1 do
for y:=1 to n-x do
if a[y] >= a[y+1] then begin
temp:=a[y]; a[y]:=a[y+1]; a[y+1]:=temp;
end;
end;
procedure lsearch;
begin
ub:=1; lb:=n;
repeat
mid:=(ub+lb) div 2;
if no>a[mid] then ub:=mid else
if no<a[mid] then lb:=mid else
if no=a[mid] then write('Found ')
until no=a[mid];
writeln('at the position ',mid)
end;
Begin
randomize;
clrscr;
n:=10;
enter;
sort;
for x:=1 to n do writeln(x,a[x]:4);
write('Enter the mark to search ='); readln(no);
lsearch;
End.