张
张辉明
Unregistered / Unconfirmed
GUEST, unregistred user!
代码一:网上的的代码(http://www.moon-soft.com/doc/30984.htm)
5.二分查找法.对已排序序列进行查找
function BinarySearch(SearchNum:array of integer;x,n:integer):integer;//序列,值,序列长度
var left,right,mid:integer;
begin
result:=-1;
left:=0;right:=n-1;
while(left<=right)do
begin
mid:=(left+right) div 2;
if x=SearchNum[mid] then
begin
result:=mid;
exit;
end;
if x>SearchNum[mid] then left:=mid+1
else right:=mid-1
end;
end;
方法二:(某开源软件的代码)
function TList.BinarySearch(Compare: TListFindCompareMethod; const Value;
var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := Compare(Items, Value);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
Result := True;
end;
end;
Index := L;
end;
如果是你怎么写呢?要求易懂,效率高。还是用二分法。
5.二分查找法.对已排序序列进行查找
function BinarySearch(SearchNum:array of integer;x,n:integer):integer;//序列,值,序列长度
var left,right,mid:integer;
begin
result:=-1;
left:=0;right:=n-1;
while(left<=right)do
begin
mid:=(left+right) div 2;
if x=SearchNum[mid] then
begin
result:=mid;
exit;
end;
if x>SearchNum[mid] then left:=mid+1
else right:=mid-1
end;
end;
方法二:(某开源软件的代码)
function TList.BinarySearch(Compare: TListFindCompareMethod; const Value;
var Index: Integer): Boolean;
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := Count - 1;
while L <= H do
begin
I := (L + H) shr 1;
C := Compare(Items, Value);
if C < 0 then L := I + 1 else
begin
H := I - 1;
if C = 0 then
Result := True;
end;
end;
Index := L;
end;
如果是你怎么写呢?要求易懂,效率高。还是用二分法。