枫
枫
Unregistered / Unconfirmed
GUEST, unregistred user!
排序2000个数据,1分钟都没跑完。
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
function partition(p,r:integer;var L:TStringList):Integer;
procedure Quick_Sort(p,r:Integer;var L:TStringList);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Quick_Sort(p,r:Integer;var L:TStringList);
const
e=12;
var
q:Integer;
begin
if p>=r then
Exit
else
begin
q:=partition(p,r,L);//将L[p..r]分解为L[p..q]和L[q+1..r]两部分
memo2.Lines:=l;
Quick_Sort(p,q,L);
//递归排序L[p..q]
Quick_Sort(q+1,r,L);//递归排序L[q+1..r]
end;
end;
function TForm1.partition(p,r:integer;var L:TStringList):Integer;
var
i,j,
pivot:Integer;
tmp:string ;
begin
pivot:=(r + p) shr 1;
//在L[p..r]中选择一个支点元素pivot
i:=p-1;
j:=r+1;
while Truedo
begin
Application.ProcessMessages;
repeat Dec(j) until CompareText(L.Strings[j],L.Strings[pivot])<=0;
//移动左指针
repeat inc(i) until CompareText(L.Strings,L.Strings[pivot])>=0;
//移动右指针
if i< j then
//交换L和L[j]
begin
tmp:=L.Strings;
L.Strings:=L.Strings[j] ;
L.Strings[j]:=tmp;
end
else
if j<>r then
begin
Result:=j;
break;
//返回j的值作为分割点
end
else
begin
Result:=j-1;
//返回j前一个位置作为分割点
break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Tmp:TStringList;
begin
Tmp:=TStringList(memo1.Lines);
Quick_Sort(0,Tmp.Count-1,Tmp);
memo2.Lines:=Tmp;
end;
procedure TForm1.Button2Click(Sender: TObject);
//随机产生2000个数据
var
i,j:Integer;
begin
Randomize;
for i:=0 to 2000do
begin
j:=Random(100000);
if memo1.Lines.IndexOf(IntToStr(j))=-1 then
memo1.Lines.Add(IntToStr(j));
end;
end;
end.
代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
function partition(p,r:integer;var L:TStringList):Integer;
procedure Quick_Sort(p,r:Integer;var L:TStringList);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Quick_Sort(p,r:Integer;var L:TStringList);
const
e=12;
var
q:Integer;
begin
if p>=r then
Exit
else
begin
q:=partition(p,r,L);//将L[p..r]分解为L[p..q]和L[q+1..r]两部分
memo2.Lines:=l;
Quick_Sort(p,q,L);
//递归排序L[p..q]
Quick_Sort(q+1,r,L);//递归排序L[q+1..r]
end;
end;
function TForm1.partition(p,r:integer;var L:TStringList):Integer;
var
i,j,
pivot:Integer;
tmp:string ;
begin
pivot:=(r + p) shr 1;
//在L[p..r]中选择一个支点元素pivot
i:=p-1;
j:=r+1;
while Truedo
begin
Application.ProcessMessages;
repeat Dec(j) until CompareText(L.Strings[j],L.Strings[pivot])<=0;
//移动左指针
repeat inc(i) until CompareText(L.Strings,L.Strings[pivot])>=0;
//移动右指针
if i< j then
//交换L和L[j]
begin
tmp:=L.Strings;
L.Strings:=L.Strings[j] ;
L.Strings[j]:=tmp;
end
else
if j<>r then
begin
Result:=j;
break;
//返回j的值作为分割点
end
else
begin
Result:=j-1;
//返回j前一个位置作为分割点
break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Tmp:TStringList;
begin
Tmp:=TStringList(memo1.Lines);
Quick_Sort(0,Tmp.Count-1,Tmp);
memo2.Lines:=Tmp;
end;
procedure TForm1.Button2Click(Sender: TObject);
//随机产生2000个数据
var
i,j:Integer;
begin
Randomize;
for i:=0 to 2000do
begin
j:=Random(100000);
if memo1.Lines.IndexOf(IntToStr(j))=-1 then
memo1.Lines.Add(IntToStr(j));
end;
end;
end.