[代码]冒泡排序的过程

冒泡排序算法

procedure sort(var data:array of integer; n:integer);
var
  i, j, temp:integer;
begin
  for j:=n-1 downto 1 do
    for i:=0 to j-1 do
      begin
        //交换次序不对者
        if data[i]>data[i+1] then 
          begin
            temp:=data[i];
            data[i]:=data[i+1];
            data[i+1]:=temp;
          end;
      end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
  data: array [0..9] of integer;
  i: integer;
begin
  for i:=0 to 9 do
    data[i]:=strtoint(ListBox1.Items.Strings[i]);
  //调用冒泡排序
  sort(data,10);
  for i:=0 to 9 do
    ListBox1.Items.Strings[i]:=inttostr(data[i]);
end; 
分享到: