Autor Beitrag
Deinemudda
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 08.03.07 09:15 
Hey Leute,
ich habe ein Problem mit Bubblesort.
Er sortiert bei mir nur nach der ersten Zahl.

Kann mir jemand helfen ??

Danke im voraus :wink:


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
procedure BubbleSort(Items: TStrings);

var
 done: boolean;
 i, n: integer;
 Dummy: string;
begin
 n := Items.Count;

 repeat
   done := true;
   for i := 0 to n - 2 do
     if Items[i] > Items[i + 1] then
     begin
       Dummy := Items[i];
       Items[i] := Items[i + 1];
       Items[i + 1] := Dummy;

       done := false;
     end;
 until done;
end;


Moderiert von user profile iconGausi: Code- durch Delphi-Tags ersetzt
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 08.03.07 10:34 
Moin und :welcome: im Forum!

Schau mal hier, da wird BubbleSort entwickelt. ;)

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Do 08.03.07 10:47 
Also bei mir funktioniert das. Das Problem dürfte wahrscheinlich daran liegen, dass du Zahlen sortieren willst, aber mit Stringvergleichen arbeitest. Und bei Strings gilt nunmal '1234' ist kleiner als '9'. So wie 'Autobahnraststättentoilettenreinigungsfachkraft' kleiner ist (d.h. im Alphabet weiter vorne steht) als "Zoo".

_________________
We are, we were and will not be.
Robinator
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 275

WinXP
BDS 2006
BeitragVerfasst: Do 08.03.07 11:23 
Der algo sieht mir eigentlich auch gut aus!

Was du allerdings noch optimieren kannst ist folgendes:

Nach dem ersten durchlauf kannst du davon ausgehen, dass das letzte element entweder das größte oder das kleinste element der Liste ist(Sprich es ist sortiert). Du kannst also n nach jedem schleifendurchlauf um eins dekrementieren ;)

gruss, Rob

_________________
erare humanum est