冒泡排序(DelphiXE2)

 用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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值