Autor Beitrag
schrottig
Hält's aus hier
Beiträge: 1



BeitragVerfasst: Di 21.04.09 20:35 
Muss ein Programm zum Sortieren mit Merge-Sort schreiben. Kann das sortierte Array aber nicht wieder in der ListBox ausgeben, sondern bekomm nur lauter Nuller. Kann mir jemand sagen, wo der Fehler liegt und ne Lösung geben?
Code:
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Edit1: TEdit;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Edit2: TEdit;
    Edit3: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    GroupBox3: TGroupBox;
    Button3: TButton;
    GroupBox4: TGroupBox;
    Button4: TButton;
    ListBox1: TListBox;
    GroupBox5: TGroupBox;
    Button5: TButton;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
  private
    procedure Mergesort (var List:array of integer);
  public

  end;

var
  Form1: TForm1;
  z,i: integer;

  List: ARRAY[1..10OF integer;

implementation

{$R *.dfm}

procedure TForm1.Button4Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button1Click(Sender: TObject);

begin

ListBox1.Items.Add(edit1.text);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Listbox1.Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);
var c,b,i:integer;
begin
b:=10//StrtoInt(edit2.Text)

c:=StrToInt(edit3.Text);
for i := 1 to b do
ListBox1.Items.Add(IntToStr(random(c)+1));




end;

procedure TForm1.Button5Click(Sender: TObject);

begin

 Mergesort(List[z]);
 Listbox1.Clear;
 for z:=0 to 10 do
 ListBox1.Items.Add(IntToStr(List[z]));
end;

procedure TForm1.Mergesort(var List:array of integer);
var
  laenge,x,y,z        :integer;
  h_array1, h_array2  :array of integer;

begin 
laenge:= length(List); 
Setlength (h_array1, laenge div 2); 
Setlength (h_array2,(laenge + 1div 2); 

for x:=0 to laenge div 2 - 1 do 
  h_array1[x]:=List[x]; 

for y:=0 to (laenge + 1div 2 - 1 do 
  h_array2[y]:=List[y+((laenge) div 2)];

if laenge > 2 then 
  begin 
  mergesort(h_array1); 
  mergesort(h_array2); 
  end
z:=0

while (length(h_array1) <> 0and (length(h_array2) <>0do 
  begin 
  If h_array1[0] < h_array2[0then 
    Begin 
    List[z] := h_array1[0]; 

    for x:=0 to length(h_array1)-2 do 
      h_array1[x]:=h_array1[x+1]; 

    Setlength (h_array1,length(h_array1)-1); 
    end 
  Else 
    begin 
    List[z]:=h_array2[0]; 

    for y:=0 to length(h_array2)-2 do 
      h_array2[y]:=h_array2[y+1]; 

    setlength(h_array2,length(h_array2)-1); 
    end
  z:=z+1;
  end

if length(h_array1)<>0 then
  for x:=0 to length(h_array1)-1 do 
    begin 
    List[z]:=h_array1[x]; 
    z:=z+1
    end

if length(h_array2)<>0 then
  for y:=0 to length(h_array2)-1 do 
    begin 
    List[z]:=h_array2[y]; 
    z:=z+1
    end

end;
end.


Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt
Xentar
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2077
Erhaltene Danke: 2

Win XP
Delphi 5 Ent., Delphi 2007 Prof
BeitragVerfasst: Di 21.04.09 20:56 

_________________
PROGRAMMER: A device for converting coffee into software.