Autor Beitrag
kenan.y
Hält's aus hier
Beiträge: 5



BeitragVerfasst: So 31.12.06 14:53 
Hallo Leute, ich bin echt keine große Leuchte in Delphi. Unser Lehrer hat uns über die Ferien die Aufgabe gestellt, in ein Listenprogramm, was wir in der Schule erstellt haben, den Quicksortalgorithmus einzubauen.
Wir haben dieses Listenprog mit Hilfe der OOP erstellt. Ich habe zwar einen Quicksortalgorythmus, aber ich habe keine Ahnung, wie ich den umändern, bzw. anpassen kann !
Ich habe mal das Projekt als Anhang !
Ich würde mich über Lösungsvorschläge freuen : )
Einloggen, um Attachments anzusehen!
kenan.y Threadstarter
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Sa 06.01.07 12:54 
So leutz, den Quicksort habe ich hinbekommen. Jetzt habe ich noch folgendes Problem !
Ich will das Bubblesortverfahren noch einbauen, den Code habe ich schon, nur habe ich Probleme beim anpassen an meine Liste !
ausblenden volle Höhe Delphi-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:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
UNIT mTListe;

interface

type

    TInhalt = String;

type
   TElement = CLASS
     // weitere Attribute
     private
        Inhalt : TInhalt;
        Naechstes : TElement;
     // weitere Methoden
     public
        procedure SetzeInhalt(pInhalt: TInhalt);
        procedure SetzeNaechstes(E : TElement);
        function GibInhalt : TInhalt;
        function GibNaechstes : TElement;
   end;


   TListe = CLASS
     // weitere Attribute
     private
        ToWurzel : TElement;
        Hilfszeiger: TElement;
        ToCursor : TElement;
        Anzahl : Integer;
     // weitere Methoden
     public
        procedure Init;
        procedure einfuegen(Element : TElement);
        procedure anfuegen(Element : TElement);
        procedure nach_vorne;
        procedure SetzeAnzahl(pAnzahl: Integer);
        function eins_vor:boolean;
        function GibAnzahl : Integer;
        function leer : Boolean;
        function GibInhalt : TInhalt;
   end;

implementation

//+---------------------------------------------------------------------
//|         TElement: Methodendefinition
//+---------------------------------------------------------------------

//-------- SetzeInhalt (public) ----------------------------------------
procedure TElement.SetzeInhalt(pInhalt: TInhalt);
   begin
      Inhalt := pInhalt
   end;

//-------- SetzeNaechstes (public) ----------------------------------------
procedure TElement.SetzeNaechstes(E : TElement);
begin
   Naechstes:=E;
end;

//-------- GibInhalt (public) ------------------------------------------
function TElement.GibInhalt : TInhalt;
begin
      result  := Inhalt
end;

//-------- GibNaechstes (public) ------------------------------------------
function TElement.GibNaechstes : TElement;
begin
      result  := Naechstes;
end;


//+---------------------------------------------------------------------
//|         TListe: Methodendefinition
//+---------------------------------------------------------------------

//-------- Init (public) -----------------------------------------------
procedure TListe.Init;
Begin
   ToWurzel:=NIL;
   Anzahl:=0;
End;


//-------- einfuegen (public) -------------------------------------
procedure TListe.einfuegen(Element : TElement);
Begin
   if Anzahl=0 then
   begin
      ToWurzel:=Element;
      ToWurzel.SetzeNaechstes(NIL);
   end

else

       if Anzahl = 1 then
       begin
if (ToCursor.gibinhalt < Element.GibInhalt) then
       ToWurzel.SetzeNaechstes(Element)
       else
       begin
       ToWurzel:= Element;
       Element.SetzeNaechstes(ToCursor);
       end;
       end
else

     begin
     ToCursor:= ToWurzel;
        if (ToWurzel.GibInhalt >= Element.GibInhalt) then
          begin
          ToWurzel:= Element;
          element.SetzeNaechstes(ToCursor); 
          end


else
       begin
       Hilfszeiger := ToWurzel;
while (ToCursor <> niland (Element.GibInhalt > ToCursor.GibInhalt)do
       begin
       Hilfszeiger:= ToCursor;
       ToCursor:= ToCursor.GibNaechstes;
      end;
      Element.SetzeNaechstes(ToCursor);
      Hilfszeiger.SetzeNaechstes(Element);
   end;
   end;
SetzeAnzahl(Anzahl+1);
End;


//-------- anfuegen (public) -------------------------------------
procedure TListe.anfuegen(Element : TElement);
Begin
   if Anzahl=0 then begin
      ToWurzel:=Element;
      ToWurzel.SetzeNaechstes(NIL);
      end
   else begin
      Element.SetzeNaechstes(ToWurzel);
      ToWurzel:=Element;
      end;
   SetzeAnzahl(Anzahl+1);
End;


//-------- nach_vorne (public) -----------------------------------------
procedure TListe.nach_vorne;
Begin
    if not leer then ToCursor:=ToWurzel;
End;



//-------- SetzeAnzahl (public) ----------------------------------------
procedure TListe.SetzeAnzahl(pAnzahl: Integer);
   begin
      Anzahl := pAnzahl
   end;

//-------- vor (public) ------------------------------------------------
function TListe.eins_vor:Boolean;
Begin
  if ToCursor.Naechstes <> NIL then
  begin
    ToCursor:=ToCursor.GibNaechstes;
    result:=true;
  end
  else
    result:=false;
End;

//-------- GibAnzahl (public) ------------------------------------------
function TListe.GibAnzahl : Integer;
   begin
      result  := Anzahl
   end;

//-------- leer (public) -----------------------------------------------
function TListe.leer : Boolean;
Begin
   leer:=Anzahl = 0;
End;

//-------- GibInhalt (public) -----------------------------------------------
function TListe.GibInhalt : TInhalt;
begin
   if not leer then GibInhalt:=ToCursor.Inhalt;
end;


end.

Das ist meine Liste, und hier ist der Bubblesort
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:
23:
24:
25:
26:
27:
28:
29:
30:
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 + 1then
      begin
        Dummy := Items[i];
        Items[i] := Items[i + 1];
        Items[i + 1] := Dummy;

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



procedure TFListe.Button1Click(Sender: TObject);
begin
BubbleSort(Listbox1.Items);
end;

end.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8549
Erhaltene Danke: 478

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 06.01.07 13:06 
Du hast ne verkettete Liste mit Quicksort sortiert? :shock: Da würde mich mal der Code interessieren, wie du das genau gemacht hast. Das es geht, ist klar, aber etwas ungewöhnlich.

Dein Grundproblem liegt daran, dass du ne echte Liste als Datenstruktur hast, man aber in aller Regel auf Array-Strukturen sortiert. Daher: Wenn du Quicksort auf der Liste zum Laufen bekommen hast, dürfte Bubblesort eigentlich kein Problem sein...

_________________
We are, we were and will not be.
raiguen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 374

WIN 2000prof, WIN XP prof
D7EP, MSSQL, ABSDB
BeitragVerfasst: Sa 06.01.07 14:17 
...und wenn doch, dann nervt man hier :roll: