mainfrm.pas

来自「用delphi实现的一些排序算法」· PAS 代码 · 共 73 行

PAS
73
字号
{-----------------------------------------------------------------------------

  作者:hujp 2006.05.09
  备注:
  审核:

  Copyright (c) 1994-2006 GrandSoft Corporation
-----------------------------------------------------------------------------}
unit MainFrm;

interface

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

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    procedure Merge(AList1, AList2, ATargetList: TList; ACompare: TCompareFunc);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

{-----------------------------------------------------------------------------
  作者:hujp 2006.05.09
  参数:AList1,AList2是两个有序列表,ATargetList是目标表
  返回:
  功能:归并排序简单模型
-----------------------------------------------------------------------------}
procedure TForm1.Merge(AList1, AList2, ATargetList: TList;
  ACompare: TCompareFunc);
var
  nIndex1, nIndex2, nIndex3: Integer;

begin
  nIndex1 := 0;
  nIndex2 := 0;
  nIndex3 := 0;
  ATargetList.Clear;
  ATargetList.Capacity := AList1.Count + AList2.Count;
  while (nIndex1 < AList1.Count) and (nIndex2 < AList2.Count) do
  begin
    if ACompare(AList1.List^[nIndex1], AList2.List^[nIndex2]) <= 0 then
    begin
      ATargetList.List^[nIndex3] := AList1.List^[nIndex1];
      Inc(nIndex1);
    end
    else begin
      ATargetList.List^[nIndex3] := AList1.List^[nIndex2];
      Inc(nIndex2);
    end;
    Inc(nIndex3);
  end;
  if nIndex1 < AList1.Count then
    Move(AList1.List^[nIndex1], ATargetList.List^[nIndex3],
      (AList1.Count - nIndex1) * Sizeof(Pointer))
  else
    Move(AList2.List^[nIndex2], ATargetList.List^[nIndex3],
      (AList2.Count - nIndex2) * Sizeof(Pointer))
end;
  
end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?