用DelphiXE2写了一下冒泡排序,新手可以参考。
---------------------------------------------------------------------------------------------------------------------------------------
unit Sorting;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
txtNum: TEdit;
cmdSort: TButton;
txtNew: TEdit;
procedure cmdSortClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function NumSort(StrNum: string; Limiter: Char): string;
var
NumStr: TStringList;
L: Integer;
Temp: Integer;
i, j: Integer;
Num: array of Integer;
begin
NumStr := TStringList.Create;
NumStr.Delimiter := Limiter;
NumStr.DelimitedText := StrNum;
L := NumStr.Count;
SetLength(Num, L - 1);
for i := 0 to L - 1 do
begin
Num[i] := StrToInt(NumStr[i])
end;
for j := 0 to L - 2 do
begin
for i := 0 to (L - 2 - j) do
begin
if Num[i] < Num[i + 1] then
begin
Temp := Num[i];
Num[i] := Num[i + 1];
Num[i + 1] := Temp;
end
end;
end;
for i := 0 to L - 1 do // 连接成字符串
if i = L - 1 then
result := result + inttostr(Num[i]) // 去除字符串最后的分割符号
else
result := result + inttostr(Num[i]) + Limiter
end;
procedure TForm1.cmdSortClick(Sender: TObject);
begin
txtNew.Text := '';
txtNew.Text := NumSort(txtNum.Text, '-');
end;
end.
![](http://hi.csdn.net/attachment/201110/31/0_13200607142QDQ.gif)