Pascal 之(冒泡排序)

Maopao排序
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DBCtrls;

type
  queue = array[1..200] of integer;

  TForm1 = class(TForm)
    btn1: TButton;
    lbl1: TLabel;
    edt1: TEdit;
    btn2: TButton;
    btn3: TButton;
    dblst1: TDBListBox;
    dbtxt1: TDBText;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure sort1(var queue1: queue; n: integer);
    procedure swap(var x, y: integer);
    { Public declarations }
  end;

var
  Form1: TForm1;
  i: integer;
  dataarray: queue;

implementation

{$R *.dfm}

procedure TForm1.sort1(var queue1: queue; n: integer);
var
  k, j: integer;
begin
  for k := 1 to n - 1 do
  begin
    for j := 1 to n - k do
      if queue1[j] < queue1[j + 1] then
      begin
        swap(queue1[j], queue1[j+1]);
      end;
  end
end;

procedure TForm1.swap(var x, y: integer);
var
  temp:integer;
begin
  temp := x;
  x := y;
  y := temp;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  j: integer;
begin
  dbtxt1.Caption := '排序后: ';
  sort1(dataarray, i);
  for j := 1 to i do
    dbtxt1.Caption := dbtxt1.Caption + ' ' + IntToStr(dataarray[j]);
  i := 0;
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  i := i + 1;
  dataarray[i] := StrToInt(edt1.Text);
  dblst1.Items.Add(edt1.Text);
  edt1.Text := '';
  edt1.SetFocus;
end;

procedure TForm1.btn3Click(Sender: TObject);
begin
Close;
end;
end.

 

转载于:https://www.cnblogs.com/anbylau2130/archive/2012/12/07/2806841.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值