Delphi 大小排序算法代码


本文整理自网络,侵删。

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure SortShell(var aSort: array of Word);
var
  iI, iJ, iK,
  iSize: Integer;
  wTemp: Word;
begin
  iSize := High(aSort);
  iK := iSize shr 1;
  while iK > 0 do begin
    for iI := 0 to iSize - iK do begin
      iJ := iI;
      while (iJ >= 0) and (aSort[iJ] > aSort[iJ + iK]) do begin
        wTemp := aSort[iJ];
        aSort[iJ] := aSort[iJ + iK];
        aSort[iJ + iK] := wTemp;
        if iJ > iK then
          Dec(iJ, iK)
        else
          iJ := 0
      end;
    end;
    iK := iK shr 1;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);

  procedure AddToLB(const Nums: array of Word; LB: TListBox);
  var
    I: Integer;
  begin
    for I := Low(Nums) to High(Nums) do
      LB.Items.Add(IntToStr(Nums[I]));
  end;

var
  I: Integer;
  Nums: array[1..50] of Word;
begin
  // Create list of random numbers and display it
  Randomize;
  for I := Low(Nums) to High(Nums) do
    Nums[I] := Random(1000);
  AddToLB(Nums, ListBox1);
  // Sort list and display sorted numbers
  SortShell(Nums);
  AddToLB(Nums, ListBox2);
end;

end.

相关阅读 >>

Delphi服务器端如何防止ddos

Delphi idhashmessagedigest 获取文件md5

Delphi 解决idhttp下载数据时程序无响应的问题

Delphi winapi: settimer、killtimer - 创建与移除高性能定时器

Delphi 删除文件的函数

Delphi 在ie上增添一个按钮

Delphi的tservice的windows�入桌面切�q

Delphi opendialog设置默认打开路径

Delphi 常用4种对话框

Delphi 给label1字加边的又一个算法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...