我才接触DELPHI,所以有个小问题想请教各位前辈.
题目:用冒泡法对输入的一组数排序(小到大)
我会写代码但我不知道该如何加在DELPHI的源代码里,搞了半天也不对,希望各位前辈能指点我一下,谢谢
源代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type queue = array[1..10] of integer;
TForm1 = class(TForm)
Labts: TLabel;
labresult: TLabel;
dataedit: TEdit;
datalist: TListBox;
input: TButton;
sort: TButton;
close: TButton;
procedure sort1(var queue1:queue;n:integer);
procedure swap(x,y:integer);
procedure inputClick(Sender: TObject);
procedure sortClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
i:integer;
dataarray:queue;
implementation
procedure sort1(var queue1:queue;n:integer);
var k,j:integer;
flag:boolean;
begin
for k:=1 to n-1 do
begin
flag:= true;
for j:=1 to n-k do
if queue1[j]<queue1[j-1] then
begin
flag:=false;
swap(queue1[j],queue1[j+1]);
end;
if flag then
exit;
end;
end;
procedure swap (var x,y:integer);
var temp:integer;
begin
temp:=x;
x:=y;
y:=temp;
end;
{$R *.dfm}
procedure TForm1.inputClick(Sender: TObject);
begin
i:=i+1;
dataarray[i]:=strtoint(dataedit.Text);
datalist.Items.Add(dataedit.Text);
dataedit.Text'';
dataedit.SetFocus;
end;
procedure TForm1.sortClick(Sender: TObject);
var j:integer;
begin
labresult.caption :='这组数从小到大排列为:'+chr(13);
sort1(dataarray,i);
for j:=1 to i do
labresult.caption:=labresult.caption+''+inttostr(dataarray[j]);
i:=0
end;
end.