Einzelnen Beitrag anzeigen

Volker Z.

Registriert seit: 3. Dez 2012
Ort: Augsburg, Bayern, Süddeutschland
419 Beiträge
 
Delphi XE4 Ultimate
 
#8

AW: BubbleSort Problem

  Alt 8. Dez 2013, 03:22
Hallo,

naja, das ist schon klar, wenn Du denn BubbleSort erst nach der Zuweisung an ListBox1 ausführst, dann kann das natürlich nicht funktionieren. Klatsch Dir doch mal noch ein weiteren TButton auf Dein Formular und versuch es so:
Delphi-Quellcode:
procedure TForm1.BubbleSort;
var
 i, j : Integer;

 procedure Swap (const Index0, Index1 : Integer);
 var
   i0, i1 : Integer;
   s : string;
 begin
   i0 := StrToInt (ListBox1.Items [Index0]);
   i1 := StrToInt (ListBox1.Items [Index1]);
   if i0 > i1 then
     begin
       s := ListBox1.Items [Index0];

       ListBox1.Items [Index0] := ListBox1.Items [Index1];
       ListBox1.Items [Index1] := s
     end
 end;

begin
 for i := 0 to ListBox1.Items.Count - 1 do
   for j := ListBox1.Items.Count - 1 downto i do
     Swap (i, j)
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items.Add (IntToStr (Random (100) - 1));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  BubbleSort;
  ListBox2.Items.Assign (ListBox1.Items)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize
end;
Gruß
Volker Zeller
  Mit Zitat antworten Zitat