program binarne_hladanie_1;
uses crt;
var n,i,hh,dh,s,h: integer;
a: array [1..1000] of integer;

PROCEDURE vylepsenabublina;
var j,p: integer;
  vymena: boolean;

begin
  randomize;
  writeln('Zadaj pocet prvkov pola:');
  readln(n);
  for i:= 1 to n do
    a[i]:=random(100)+1;
  repeat
    vymena:=false;
    for j:=1 to n-1 do
      if a[j] > a[j+1] then
        begin
          p:=a[j]; a[j]:=a[j+1]; a[j+1]:=p;
          vymena:=true;
        end;
  until vymena=false;
  writeln;
  writeln('zoradene pole: ');
  for j:=1 to n do
    write(a[j],'  ');
  writeln;
end;



begin
  clrscr;
  
  vylepsenabublina;
  write('Zadaj prvok ktory chces v poli najst: ');
  readln(h);
  if h>a[n] then writeln('nie')
  else begin
    hh:=a[n];
    dh:=a[1];
    s:=n+1;
    a[s]:=n+1;
    while (a[s]<>h) and (dh<=hh) do begin
      s:=(dh+hh) div 2;
      if a[s] > h then hh:=s-1
      else dh:=s+1;
    end;
    if a[s]=h then writeln('ano')
    else writeln('nie');
  end;
  readln;
end.
