delphi dynamic Array 排序 快速排序,选择排序,冒泡排序

 
{
这段代码存在的目的不是在研究这3个排序的算法,
这段代码存在的目的是如何用这3个排序函数,并且选出一个排序速度最快的函数 面对实际应用
 
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Memo2: TMemo;
    Edit1: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


{选择排序} 
procedure selectrank(var a:array of integer;maxtomin:boolean); 
var i,j,t:integer; 
begin 
   if maxtomin then                 //由大到小 
     begin 
       for i:=low(a) to high(a)-1 do 
         for j:=i+1 to high(a) do
             if a[i]<a[j] then 
                begin 
                   t:=a[i]; 
                   a[i]:=a[j]; 
                   a[j]:=t; 
                end; 
     end 
   else                     //由小到大 
    begin 
     for i:=low(a) to high(a)-1 do 
         for j:=i+1 to high(a) do 
             if a[i]>a[j] then    //关键区别是符号 
                begin 
                   t:=a[i]; 
                   a[i]:=a[j]; 
                   a[j]:=t; 
                end; 
     end; 
end; 

{快速排序} 
procedure speedrank(var a:array of integer;maxtomin:boolean); 
var i,j,t:integer; 
    flag:boolean; 
begin 
  if maxtomin then                  //由大到小 
     begin 
        i:=1; 
        repeat 
          flag:=true; 
          for j:=low(a) to high(a)-i do 
             if a[j]<a[j+1] then 
                begin 
                   t:=a[j]; 
                   a[j]:=a[j+1]; 
                   a[j+1]:=t; 
                   flag:=false;
                end; 
           i:=i+1; 
        until flag; 
     end//end if 
  else                        //由小到大 
    begin 
      i:=1; 
      repeat 
         flag:=true; 
         for j:=low(a) to high(a)-i do 
            if a[j]>a[j+1] then   //关键点是大于好还是小于号 
               begin 
               t:=a[j]; 
               a[j]:=a[j+1]; 
               a[j+1]:=t; 
               flag:=false; 
               end; 
               i:=i+1; 
      until flag; 
    end; 
end; 

{冒泡排序法}
procedure bublerank(var a:array of integer;maxtomin:boolean); 
var i,j,t:integer; 
begin 
  if maxtomin then             //由大到校排序 
    begin 
      for i:=low(a) to high(a)-1 do 
          for j:=low(a) to high(a)-i-1 do 
                  if a[j]<a[j+1] then 
                     begin 
                       t:=a[j]; 
                       a[j]:=a[j+1]; 
                       a[j+1]:=t; 
                     end; 
    end 
  else                         //由小到大排序 
   begin 
    for i:=low(a) to high(a)-1 do 
        for j:=low(a) to high(a)-i-1 do 
                if a[j]>a[j+1] then 
                   begin 
                     t:=a[j]; 
                     a[j]:=a[j+1]; 
                     a[j+1]:=t; 
                   end; 
   end; 
end; 


var A:  Array of Integer;{共有部分}
{1/2产生13个随机数组}
procedure TForm1.Button1Click(Sender: TObject);
var  i:integer;{1/2产生随机数并给数组赋值}
begin
   Memo1.Clear;
  SetLength(A,StrToInt(Edit1.Text));//动态数组的长度
  for i:=Low(A) to High(A) do
   begin
      Randomize; 
      a[i]:=Random(100);
      Memo1.Lines.Add(IntToStr(a[i]));{}
   end; 
end; 

 {2/2排序}
procedure TForm1.Button2Click(Sender: TObject);
var  i:integer; {2/2重小到大排序->取最小值}
begin
  Memo2.Clear; //
  for i:=Low(A) to High(A) do
   begin
      speedrank(a,False); {排序}
      Memo2.Lines.Add(Format('%d',[a[i]])); {显示结果}
   end;
   if A<>nil then  Button2.caption:=Format('min=%d',[a[0]]);{取出最小值:从小到大排序的最终结果 最小值就是第1位}
   A:=nil;{排完序后立即释放动态数组}
end;


end.




附件列表

    原文地址:https://www.cnblogs.com/xe2011/p/2518883.html