2018-10-02 快速排序
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure QuickSort(var N: array of integer; L, R: integer);
var
m,I, J, IntTemp: integer;
begin
if L < R then
begin
I := L;
J := R;
IntTemp := N[I];
while I < J do
begin
while (I < J) and (N[J] >= IntTemp) do
begin
J := J - 1;
end;
if I < J then
N[I] := N[J];
while (I < J) and (N[I] <= IntTemp) do
begin
I := I + 1;
end;
if I < J then
N[J] := N[I];
end;
N[I] := IntTemp;
QuickSort(N, L, I - 1);
QuickSort(N, I + 1, R);
end;
end;
procedure TForm3.Button1Click(Sender: TObject);
var
m,I, J: integer;
IntTemp:string;
n: array of integer; //定义一个动态数组;
// n: array [0..3] of integer; 静态数组的定义方式
begin
setlength(n,memo1.Lines.count); //给动态数组分配空间;
for m:=0 to memo1.Lines.Count-1 do
begin
n[m]:= strtoint(memo1.Lines[m]); // 把MEMO1每行的值压入对应的数组变量
end;
{
n[0]:=4;
n[1]:=7;
静态数组的赋值方式
}
QuickSort(n,0, memo1.Lines.count-1); //调用排序函数; 写入要排序的数组,起点值,终点值。
for m:=0 to memo1.Lines.Count-1 do
begin
memo2.Lines.Add(inttostr(n[m])); //将数组内的排序结果展示到MEMO2;
end;
end;
end.