Originally Posted by
Robert Kosek
Yuriy, can you give me a working example of a binary search? I can't honestly compare it to one without knowing what it should look like.
Sure, here's an example code from our GameScripts.pas:
Code:
function TScriptObject.GetNode(ID: Cardinal): TScriptObject;
var
Lo, Hi, Mid: Integer;
begin
if (SearchDirty) then UpdateSearchList();
Result:= nil;
Lo:= 0;
Hi:= SearchCount - 1;
while (Lo <= Hi) do
begin
Mid:= (Lo + Hi) div 2;
if (SearchList[Mid].ID = ID) then
begin
Result:= SearchList[Mid];
Break;
end;
if (SearchList[Mid].ID > ID) then Hi:= Mid - 1 else Lo:= Mid + 1;
end;
end;
Remember that the list must be sorted for binary search to work.
Bookmarks