i,j,temp:integer;
for i:=0 to n-1 do
 for j:=i+1 to n do
  begin
   if a[i]>a[j] then
    begin
     temp := a[i];
     a[i] := a[j];
     a[j] := temp;
    end;
  end;
請大家看看我上面寫的是不是標准的delphi冒泡方法啊?

解决方案 »

  1.   

    var
      i, j, temp: Integer; 
    begin
      for i := 0 to n - 1 do
      begin
        for j := n - 1 downto i + 1 do 
        begin 
          if a[j] < a[j - 1] then 
          begin 
            temp := a[j]; 
            a[j] := a[j - 1]; 
            a[j - 1] := temp; 
          end; 
        end; 
      end;
    end;
      

  2.   

    一个button 两个listbox
    var
      i, j, empty: integer;
      line: array of integer; //定义了一个数组
    begin
      SetLength(line, listbox1.Items.Count); //动态数组setlength(定义的数组名,个数)
      for i := 0 to ListBox1.Items.Count - 1 do
      begin
        line[i] := StrToInt(listbox1.Items.strings[i]);
      end;
      for i := 0 to ListBox1.Items.count - 1 do
      begin //两重循环实现冒泡排序
        for j := 0 to ListBox1.Items.count - 2 - i do
        begin
          if line[j] < line[j + 1] then
          begin
            empty := line[j];
            line[j] := line[j + 1];
            line[j + 1] := empty;
          end;
        end;
      end;
      for i := 0 to ListBox1.Items.count - 1 do
      begin //输出数组
        listbox2.items.Add(inttostr(line[i]));
      end;