Советы по Delphi


         

Функция бинарного поиска


function FoundByBinarySearch
(LowIdx,HighIdx : LongInt;var Result : LongInt;const GoalIs : CompareFunc;var Data;var Goal) : Boolean;varCompVal : CompareResults;beginFoundByBinarySearch := FALSE;
if HighIdx < LowIdx thenExit;
Result := LowIdx + ((HighIdx-LowIdx) div 2);CompVal := GoalIs(Result, Data, Goal);
if CompVal = BinEqual thenFoundByBinarySearch := TRUEelse if (LowIdx < HighIdx) thenbeginif CompVal = BinLess thenHighIdx := Result-1else {CompVal = BinGreater}LowIdx := Result+1;FoundByBinarySearch := FoundByBinarySearch(LowIdx, HighIdx, Result, GoalIs, Data, Goal)endelse if (CompVal = BinLess) thenDec(Result)end; { function FoundByBinarySearch }

[000406]



Содержание  Назад  Вперед