home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / Chip_2000-02_cd.bin / zkuste / Delphi / navody / tt / binsearch.txt next >
Text File  |  1999-11-22  |  1KB  |  37 lines

  1. function FoundByBinarySearch
  2.         (    LowIdx,
  3.               HighIdx : LongInt;
  4.         var   Result  : LongInt;
  5.         const GoalIs  : CompareFunc;
  6.         var   Data;
  7.         var   Goal
  8.         ) : Boolean;
  9.     var
  10.         CompVal : CompareResults;
  11.     begin
  12.         FoundByBinarySearch := FALSE;
  13.  
  14.         if HighIdx < LowIdx then
  15.             Exit;
  16.  
  17.         Result  := LowIdx + ((HighIdx-LowIdx) div 2);
  18.         CompVal := GoalIs(Result, Data, Goal);
  19.  
  20.         if CompVal = BinEqual then
  21.             FoundByBinarySearch := TRUE
  22.           else if (LowIdx < HighIdx) then
  23.             begin
  24.             if CompVal = BinLess then
  25.                 HighIdx := Result-1
  26.               else {CompVal = BinGreater}
  27.                 LowIdx  := Result+1;
  28.             FoundByBinarySearch := FoundByBinarySearch(
  29.                 LowIdx, HighIdx, Result, GoalIs, Data, Goal)
  30.             end
  31.           else if (CompVal = BinLess) then
  32.             Dec(Result)
  33.     end; { function FoundByBinarySearch }
  34.  
  35.  
  36.  
  37.