《Delphi 算法与数据结构》学习与感悟[1]: 通过 "顺序查找" 与 "二分查找" 说明算法的重要性...

本文通过实际测试对比了顺序查找与二分查找在不同数据规模下的效率。实验使用了Delphi语言进行实现,通过QueryPerformanceCounter来精确测量两种算法的时间消耗,并分析了在百万级数据集上的表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试效果图:

26153747_yLK1.gif
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{顺序查找函数}
function SeqSearch(List: TStringList; const str: string): Integer;
var
  i: Integer;
begin
  for i := 0 to List.Count - 1 do
    if CompareText(List[i], str) = 0 then begin Result := i; Exit; end;
  Result := -1;
end;

{二分查找函数; 二分查找只能针对有序列表}
function BinarySearch(List: TStringList; const str: string): Integer;
var
  L,R,M: Integer;
  CompareResult: Integer;
begin
  Result := -1;
  L := 0;
  R := List.Count - 1;

  while L <= R do
  begin
    M := (L + R) div 2;
    CompareResult := CompareText(List[M], str);
    if CompareResult < 0 then L := M + 1 else
    if CompareResult > 0 then R := M - 1 else
    begin
      Result := M;
      Exit;
    end;
  end;
end;

{对比测试}
procedure TForm1.Button1Click(Sender: TObject);
var
  TestList: TStringList;
  i: Integer;
  n1,n2: Int64;
  Count1,Count2: Integer;
  s: string;
const
  num = 1000000; {准备测试百万个数据}
begin
  TestList := TStringList.Create;
  for i := 0 to num-1 do TestList.Add(IntToHex(i,8)); {准备有序的测试值列表}

  Memo1.Clear;
  Count1 := 0;
  Count2 := 0;

  {搞 10 实验}
  for i := 0 to 9 do
  begin
    {产生范围内的随机字串}
    Randomize;
    s := IntToHex(Random(num),8);

    {顺序查找}
    QueryPerformanceCounter(n1);
    SeqSearch(TestList, s);
    QueryPerformanceCounter(n2);
    Memo1.Lines.Add(IntToStr(n2-n1)+ #9);
    Count1 := Count1 + (n2-n1);

    {二分查找}
    QueryPerformanceCounter(n1);
    BinarySearch(TestList, s);
    QueryPerformanceCounter(n2);
    Memo1.Lines[i] := Memo1.Lines[i] + IntToStr(n2-n1);
    Count2 := Count2 + (n2-n1);
  end;

  Memo1.Lines.Add('----------------');
  Memo1.Lines.Add('平均值:');
  Memo1.Lines.Add(IntToStr(Count1 div 10)+ #9 + IntToStr(Count2 div 10));
  Memo1.Lines.Add('----------------');
  Memo1.Lines.Insert(0, '顺序'#9'二分');

  TestList.Free;
end;

end.

 
 
 
 
 

 

 

  
二分查找太快了, 用 GetTickCount 测试不出来, 只好使用 QueryPerformanceCounter;
另外 TStringList.Find 方法也是使用了 "二分查找" 的办法.

转载于:https://my.oschina.net/hermer/blog/320515

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值