如有重复,可处理,也可不处理,不稳定排序与稳定排序。
冒泡效率很差,一般不用,快速排序较好。我写了冒泡,如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure BublSort(var aStrings: TStrings);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BublSort(var aStrings: TStrings);
var
i,j: Integer;
temp: string;
begin
if Assigned(aStrings)then
begin
for i:= aStrings.Count-2do
wnto 0do
begin
for j:= 0 to ido
begin
if aStrings[j]>aStrings[j+1] then
begin
//aStrings.Exchange(j,j+1);
temp:= aStrings[j+1];
aStrings[j+1]:= aStrings[j];
aStrings[j]:= temp;
end;
end;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
aStrings: TStrings;
begin
aStrings:= TStringList.Create;
try
aStrings.Delimiter:= ',';
aStrings.DelimitedText:= '2,3,7,4,1';
//TStringList(aStrings).Sort;//调用tstringList封装的sort,快速排序,效率好
BublSort(aStrings);//我写的冒泡排序函数
Memo1.Lines.Assign(aStrings);
finally
aStrings.Free;
end;
end;
end.