Autor Beitrag
Cash
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 17.03.03 18:28 
Hi@all

Ich möchte gern Strings in einer Listbox sortieren. Die items sind "Eins" , "ZWei" , "drei" , usw. bis "zehn".
Links daneben ist ein Button, der beim clicken die Liste sortieren soll.
Es soll aber die rekrusive Form vom QUICKSORT-Verfahren angewendet werden.
So sieht mein Quelltext aus es fehlen einige Dinge;

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
procedure TFrmQuicksort.BTTStartClick(Sender: TObject);
begin

end;
Function Partition( l,r : Integer ) : Integer;
var v,t,i,j : Integer;
Begin
  v:= Data[r];
  i:= l-1;
  j:= r;
  Repeat
    Repeat inc( i ); Until (Data[i] >= v);
    Repeat dec( j ); Until (Data[j] <= v); 
    t:= Data[i]; Data[i]:= Data[j]; Data[j]:= t; 
  Until (j<=i);

  Data[j]:= Data[i]; Data[i]:= Data[r]; Data[r]:= t; 
  Result:= i; 
End;

Procedure QuickSort( l,r : Integer ); 
var i : Integer; 
Begin
  If (r > l) Then
  Begin
    i:= Partition( l, r); 
    QuickSortRekursiv( l, i-1 ); 
    QuickSortRekursiv( i+1, r );
  End; 
End;


zu dem hoffe ich das einer von euch eine kleine demo machen könnte, das Project zippen und mir per E-Mail schicken könnte :)

danke für jede Hilfe.[/code]

_________________
Ein Sieger gewinnt etwas, ein 'Geschlagener' lernt eine Menge dazu :)
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mo 17.03.03 19:08 
Hier gibt es eine ganz gute Seite zu Sortierverfahren in Delphi.

MfG,
Peter

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
mars
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 238

Debian Woody, Win 2000, Win XP
D7 Ent, Kylix 3
BeitragVerfasst: Mo 17.03.03 22:05 
Zudem hast du unter Demos auch ein Beispielprogrammm, welches Quick-, Bubble- und noch irgendein -Sort gegeneinander antreten lässt. Ich glaube, es ist unter Threads zu finden.
Ich weiss nicht, wie dein Programm aufgebaut ist, aber vielleicht hilft es dir auch zu wissen, dass StringList schon ein eingebautes Quicksort und CustomSort hat...
Cash Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Mo 17.03.03 22:24 
Hier der Quelltext den ich eingegeben habe:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
procedure TFrmQuicksort.BTTStartClick(Sender: TObject);
begin
Quicksort;
end;
Procedure Quicksort (l,r: integer);;

var i,j,Mitte,Merke : Integer;

begin
  i:=l;
  j:=r;
  Mitte:=Listbox1.items[(l+r) div 2];
  repeat
   while Listbox1.items[i]<Mitte do Inc(i);
   while Mitte<Listbox1.items[j] do Dec(j);
   if i<=j then
     begin
       Merke:=Listbox1.items[i];
       Listbox1.items[i]:=Listbox1.items[j];
       Listbox1.items[j]:=Merke;
       Inc(i);
       Dec(j);
     end;
  until i>j;
  if l<j then QuickSort (l,j);
  if i<r then Quicksort (i,r);

end;


Meine Frage: Wo ist der Fehler? Denn sobald ich das Programm starte, wird es genauso schnell wieder beendet, ohne das er mir einen Fehler anzeigt. Woran liegt das?

_________________
Ein Sieger gewinnt etwas, ein 'Geschlagener' lernt eine Menge dazu :)
Luncustaf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 324

Win 2k
D7 Ent.
BeitragVerfasst: Di 18.03.03 09:19 
Cash hat folgendes geschrieben:
Hier der Quelltext den ich eingegeben habe:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
procedure TFrmQuicksort.BTTStartClick(Sender: TObject);
begin
Quicksort(l,r); //übergib hier mal für l und r zwei werte da die andere procedure ja sonst nichts zu tun hat.
end;
Procedure Quicksort (l,r: integer);;

var i,j,Mitte,Merke : Integer;

begin
  i:=l;
  j:=r;
  Mitte:=Listbox1.items[(l+r) div 2];
  repeat
   while Listbox1.items[i]<Mitte do Inc(i);
   while Mitte<Listbox1.items[j] do Dec(j);
   if i<=j then
     begin
       Merke:=Listbox1.items[i];
       Listbox1.items[i]:=Listbox1.items[j];
       Listbox1.items[j]:=Merke;
       Inc(i);
       Dec(j);
     end;
  until i>j;
  if l<j then QuickSort (l,j);
  if i<r then Quicksort (i,r);

end;


Meine Frage: Wo ist der Fehler? Denn sobald ich das Programm starte, wird es genauso schnell wieder beendet, ohne das er mir einen Fehler anzeigt. Woran liegt das?
[/b]
Cash Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Di 18.03.03 15:32 
Hi,

jetzt hab ich es so eingegeben, aber er meckert mir jetzt folgendes an:

[Error] mQuicksort.pas(33): Undeclared identifier: 'Listbox1'

Ich habe aber eine Listbox die so heisst mit Items.....
Was ist zu tun?

_________________
Ein Sieger gewinnt etwas, ein 'Geschlagener' lernt eine Menge dazu :)
derDoc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 623

Win Vista Prof
D2007 Prof
BeitragVerfasst: Di 18.03.03 16:50 
Ist nur so eine Idee: Da gehört ein TFrmQuicksort. (oder wie das Ding auch immer heißt) davor:

ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
private
  procedure Quicksort(l, r: integer);

.......

procedure TFrmQuicksort.Quicksort (l, r: integer); 
var 
  i, j, Mitte, Merke: integer; 
begin 
  i := l; 
  j := r; 
  Mitte := Listbox1.Items[(l + r) div 2]; 
  repeat 
    while Listbox1.Items[i] < Mitte do 
      Inc(i); 
    while Mitte < Listbox1.Items[j] do 
      Dec(j); 
    if i <= j then 
      begin 
        Merke := Listbox1.Items[i]; 
        Listbox1.Items[i] := Listbox1.Items[j]; 
        Listbox1.Items[j] := Merke; 
        Inc(i); 
        Dec(j); 
      end; 
  until 
    i > j; 
  if l < j then 
    QuickSort (l, j); 
  if i < r then 
    Quicksort (i, r); 
end;

_________________
MfG derDoc
There are only 10 types of people: those who understand binary and those who don't.