Delphi写的折半查找算法实现

{*
查找指定的值是否在当前数组中(数组已经是有序的)
*}
function SearchData(dataList: array of integer;  id: longint ): Boolean;
var
  idMid: integer;
  idLow, idHigh: integer;
begin
  idLow := 0;
  idHigh := High(dataList);

  while ( idLow <= idHigh ) do
  begin
    if idLow = idHigh then
    begin
      if dataList[ idLow ] = id then
      begin
        Result := True;
      end
      else
      begin
        Result := False;
      end;
      Exit;
    end;
    idMid := ( idLow + idHigh ) div 2;
    if dataList[ idMid ] = id then
    begin
      Result := True;
      Exit;
    end;

    if dataList[ idMid ] > id then idHigh := idMid – 1;
    if dataList[ idMid ] < id then idLow := idMid + 1;
  end;

  Result := False;
end; 

    原文作者:查找算法
    原文地址: https://blog.csdn.net/derryzhang/article/details/1560795
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞