![]() |
Bubblesort einer Listbox. Mit 3 Verknüfungen
Hallo,
ich habe 4 Listboxen nebeneinander und die sollen nach einer bestimmten Listbox geordnet werden. Das ist so das ich 4 Werte in einer Zeile habe die alle zusammengehören.
Delphi-Quellcode:
aber irgendwie will er mir keine richtigen ergebnisse ausspücken
procedure TForm1.Sort;
var i, j: integer; h,g,l,o: string; begin Progressbar2.position := 0; progressbar2.Max := Listbox2.count -1; for i := 1 to Listbox2.count -1 do Progressbar2.position := Progressbar2.position + 1; for j := 1 to ListBox2.Items.Count - 1 do begin if Listbox2.Items[j] > Listbox2.Items[j-1] then begin h := Listbox2.Items[j]; g := Listbox1.Items[j]; l := Listbox3.Items[j]; o := Listbox4.Items[j]; Listbox2.Items[j] := Listbox2.Items[j-1]; Listbox2.Items[j-1] := h; Listbox1.Items[j] := Listbox1.Items[j-1]; Listbox1.Items[j-1] := g; Listbox3.Items[j] := Listbox3.Items[j-1]; Listbox3.Items[j-1] := l; Listbox4.Items[j] := Listbox4.Items[j-1]; Listbox4.Items[j-1] := o; end; end; end; vielcith könnt ihr mir ja helfen |
Re: Bubblesort einer Listbox. Mit 3 Verknüfungen
Hallo,
Deine Funktion ist leider fehlerhaft.
Delphi-Quellcode:
Gruß
procedure TForm1.Sort;
var i, j: Integer; begin Progressbar2.Position := 0; progressbar2.Max := Listbox2.Count - 1; for i := 0 to Listbox2.Items.Count - 1 do // <- hier mit Null beginnen begin // <- hier ein begin hin Progressbar2.Position := Progressbar2.Position + 1; for j := i + 1 to ListBox2.Items.Count - 1 do // <- hier von i + 1 bis Ende zählen begin if Listbox2.Items[j] > Listbox2.Items[i] then // <- hier j mit i vergleichen begin Listbox1.Items.Exchange(i, j); // <- einfacher Listbox2.Items.Exchange(i, j); Listbox3.Items.Exchange(i, j); Listbox4.Items.Exchange(i, j); end; end; end; // <- hier ein end hin end; xaromz |
Re: Bubblesort einer Listbox. Mit 3 Verknüfungen
ja super danke.....
im Prinzip geht das jetzt auch. Nur leider zählt er bei mir immer nur die 1. Stelle. Weiss jemand woran das lieht. Die 20 sieht er als 2 und ist somit kleiner als die 3, usw ??? |
Re: Bubblesort einer Listbox. Mit 3 Verknüfungen
Liste der Anhänge anzeigen (Anzahl: 1)
Hallo,
Zitat:
Gruß xaromz |
Re: Bubblesort einer Listbox. Mit 3 Verknüfungen
Danke schon das ging auch einfach indem ich die Strings in eine zahl gewandelthabe:
jeztt folgenden. Wenn ich die ganzen Zeilen in der Listbox tauschen dauert das unheimlich lange also will ich das in Arrays machen.
Delphi-Quellcode:
procedure TForm1.Sort(List : TListbox);
var i, j: Integer; Box1, Box2, Box3, Box4: array[0..900] of string ; b1,b2,b3,b4 :string; begin For i := 0 to List.Items.Count -1 do begin Box1[i] := Listbox1.Items[i]; Box2[i] := Listbox2.Items[i]; Box3[i] := Listbox3.Items[i]; Box4[i] := Listbox4.Items[i]; end; ; i := 0; Progressbar2.Position := 0; progressbar2.Max := List.Count - 1; for i := 0 to List.Items.Count - 1 do begin Progressbar2.Position := Progressbar2.Position + 1; for j := i + 1 to List.Items.Count - 1 do begin if strtofloat(List.Items[j]) > strtofloat(List.Items[i]) then begin //--- Anfang---\\ b1 := box1[i]; box1[i] := box1[j]; box1[j] := b1; b1 := ''; //-----Ende-----\\ //--- Anfang---\\ b2 := box2[i]; box2[i] := box2[j]; box2[j] := b2; b2 := ''; //-----Ende-----\\ //--- Anfang---\\ b3 := box3[i]; box3[i] := box3[j]; box3[j] := b3; b3 := ''; //-----Ende-----\\ //--- Anfang---\\ b4 := box4[i]; box4[i] := box4[j]; box4[j] := b4; b1 := ''; //-----Ende-----\\ end; end; end; For i := 0 to List.Items.Count -1 do begin Listbox1.Items[i] := Box1[i]; Listbox2.Items[i] := Box2[i]; Listbox3.Items[i] := Box3[i]; Listbox4.Items[i] := Box4[i]; end; end; Wo ist mein fehler??? |
Re: Bubblesort einer Listbox. Mit 3 Verknüfungen
Hallo,
Dein Fehler liegt hier:
Delphi-Quellcode:
Du vergleichst ja immer die unsortierte Liste, da Du das Ergebnis erst am Ende wieder der Liste zuordnest. Bubblesort braucht aber eine sich langsam sortierende Liste.
if strtofloat(List.Items[j]) > strtofloat(List.Items[i]) then
Um das Problem mit der Geschwindigkeit zu lösen versuche mal folgendes:
Delphi-Quellcode:
Damit schaltest Du die Bildschirmaktualisierung aus, das beschleunigt ungemein. Außerdem solltest Du nicht BubbleSort verwenden, das ist so ziemlich das Langsamste was es gibt. Nimm lieber Quicksort. Ich hab mal ein Beispiel geschrieben:
procedure TForm1.Sort(List: TListBox);
var i, j: Integer; begin Progressbar2.Position := 0; progressbar2.Max := Listbox2.Count - 1; ListBox1.Items.BeginUpdate; ListBox2.Items.BeginUpdate; ListBox3.Items.BeginUpdate; ListBox4.Items.BeginUpdate; for i := 0 to List.Items.Count - 1 do begin Progressbar2.Position := Progressbar2.Position + 1; for j := i + 1 to List.Items.Count - 1 do begin if List.Items[j] > List.Items[i] then begin Listbox1.Items.Exchange(i, j); Listbox2.Items.Exchange(i, j); Listbox3.Items.Exchange(i, j); Listbox4.Items.Exchange(i, j); end; end; end; ListBox1.Items.EndUpdate; ListBox2.Items.EndUpdate; ListBox3.Items.EndUpdate; ListBox4.Items.EndUpdate; end;
Delphi-Quellcode:
Aufruf:
procedure TForm1.SortQuick(List: TListBox);
function Compare(S1, S2: String): Integer; begin // Negatives Ergebnis -> S1 ist kleiner // Positives Ergebnis -> S2 ist kleiner // Null -> Gleicher String Result := StrToInt(S1) - StrToInt(S2); end; procedure Exchange(I, J: Integer); begin // Hier wird getauscht ListBox1.Items.Exchange(I, J); ListBox2.Items.Exchange(I, J); ListBox3.Items.Exchange(I, J); ListBox4.Items.Exchange(I, J); end; procedure QuickSort(L, R: Integer); var I, J: Integer; S: String; begin repeat I := L; J := R; S := List.Items[(L + R) shr 1]; repeat while Compare(List.Items[I], S) < 0 do Inc(I); while Compare(List.Items[J], S) > 0 do Dec(J); if I <= J then begin Exchange(I, J); Inc(I); Dec(J); end; until I > J; if L < J then QuickSort(L, J); L := I; until I >= R; end; begin QuickSort(0, List.Items.Count - 1); end;
Delphi-Quellcode:
Die Funktion Compare kannst Du so anpassen, dass sie Deinen Bedürfnissen entspricht.
procedure TForm1.Button1Click(Sender: TObject);
begin // Update ausschalten ListBox1.Items.BeginUpdate; ListBox2.Items.BeginUpdate; ListBox3.Items.BeginUpdate; ListBox4.Items.BeginUpdate; // Sortieren SortQuick(ListBox1); // Update einschalten ListBox1.Items.EndUpdate; ListBox2.Items.EndUpdate; ListBox3.Items.EndUpdate; ListBox4.Items.EndUpdate; end; Gruß xaromz |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:52 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz