{ Sequentielle Suche & Binaere Suche      }
{ Autor: Dipl.-Ing. Thomas Speiser        }
{ Source from: http://Codes.TurboTools.de }

program suche;
uses Crt;
const n = 10;
type feld = array[1..n] of integer;
var  zahl: feld;
     s,i: integer;

function SequentialSearch (k: integer): integer;
var i: integer;
begin
 zahl[1] := k;
 inc(i);
  repeat
   dec(i)
  until zahl[i] = k;
 SequentialSearch := i;
end;

{ Laeuft nach dem Teile und Herrsche - Prinzip }
function BinarySearch(a: feld; l,r,k : integer): integer;
var m: integer;
begin
if l > r then BinarySearch := 0
 else
 begin
    m := (l+r) div 2;
  if k < a[m] then BinarySearch := BinarySearch(a,l,m-1,k)
  else
  if k > a[m] then BinarySearch := BinarySearch(a,m+1,r,k)
  else
  BinarySearch:=m;
 end;
end;

begin
TextMode(CO80 xor Lo(LastMode)+Font8x8 xor LASTMODE);
randomize;
ClrScr;
writeLN('S U C H E N - D E M O ! ! !');
writeLN;
for i:=1 to n do begin zahl[i]:=i; write(i); end;
writeLN;
writeLN('Nach was soll gesucht werden: ');
readLN(s);
writeLN(BinarySearch(zahl,1,n,s));
readLN;
end.