delphi直接插入法排序示例  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi直接插入法排序示例


插入法排序

Delphi直接插入法排序示例,将一数组按插入法排序的方法进行有序排列,可视化操作窗口,如下图所示,点击“排序”按钮即可实现排序功能。

插入法排序

Delphi插入法排序代码如下:

 

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;

type

  TForm1 = class(TForm)

    Button1: TButton;

    ListBox1: TListBox;

    procedure Button1Click(Sender: TObject);

    procedure FormShow(Sender: TObject);

  private

  procedure compositor(var L:array of integer);

    { Private declarations }

  public

    { Public declarations }

  end;

var

  Form1: TForm1;

  a:Array[0..10] of integer;

  s1,s2:string;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.compositor(var L: array of integer);

var

i,j:integer;

v:integer;

begin

  for i:=low(L)+1 to high(L) do

    begin

      v:=L[i];

      j:=i;

      while (j<>low(L))and(L[j-1]

      begin

        L[j]:=L[j-1];//移动元素

        j:=j-1;

      end;

      L[j]:=v;//插入元素

    end;

end;

procedure TForm1.Button1Click(Sender: TObject);

var

  i:integer;

begin

  ListBox1.Items.Clear;

  ListBox1.Items.Add('没有排序的数组:');

  ListBox1.Items.Add(s1);

  ListBox1.Items.Add('排序后的数组:');

  compositor(a);

  s2:='';

  for i:=0 to 10 do

  begin

    s2:=s2+IntToStr(a[i])+',';

  end;

   ListBox1.Items.Add(s2);

end;

procedure TForm1.FormShow(Sender: TObject);

var

  i:Integer;

begin

  s1:='';

  for i:=0 to 10 do

  begin

    a[i]:=i*2+random(20)-5;

    s1:=s1+IntToStr(a[i])+',';

  end;

  ListBox1.Items.Add('没有排序的数组:');

  ListBox1.Items.Add(s1);

end;

end.


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.059915781021118 seconds