PROGRAM bubble_sort;//冒泡排序
CONST N = 10;
VAR A : ARRAY[ 1..N ] OF INTEGER;
T, I, J : INTEGER;
FLAG :BOOLEAN;
BEGIN
WRITE( '输入10个整数:' );
FOR I:= 1 TO N DO READ( A[I] );
READLN; WRITELN;
I:=1;
REPEAT
FLAG:= TRUE;
FOR J:= 1 TO N-I DO
IF A[J] > A[J+1] THEN
BEGIN
T:=A[J]; A[J]:=A[J+1]; A[J+1]:=T;
FLAG:= FALSE
END;
I:=I+1;
UNTIL FLAG ;
WRITE( '输出排序结果:' );
FOR I:= 1 TO N DO WRITE( A[I]:3 );
WRITELN; READLN;
END.
//***********************************************//
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
temparray:array[0..9] of Integer;
i,j,temp:Integer;
s:string;
begin
temparray[0]:=StrToInt(edit1.Text);
temparray[1]:=StrToInt(edit2.Text);
temparray[2]:=StrToInt(edit3.Text);
temparray[3]:=StrToInt(edit4.Text);
temparray[4]:=StrToInt(edit5.Text);
temparray[5]:=StrToInt(edit6.Text);
temparray[6]:=StrToInt(edit7.Text);
temparray[7]:=StrToInt(edit8.Text);
temparray[8]:=StrToInt(edit9.Text);
temparray[9]:=StrToInt(edit10.Text);
i:=0;
while i<High(temparray) do begin
if temparray[i]>temparray[i+1] then
begin
temp:=temparray[i];
temparray[i]:=temparray[i+1];
temparray[i+1]:=temp;
i:=0;
end
else i:=i+1;
end;
s:='';
for j:=0 to 9 do
s:=s+IntToStr(temparray[j])+' ';
label2.Caption:=s;
end;
end.