Entwickler-Ecke

Sonstiges (Delphi) - Array sortieren


sirchillalot - Sa 23.11.02 13:55
Titel: Array sortieren
Wie kann ich 10 integerzahlen die in eimem 2 dim array stehen sortieren und sie in die 2 dimesion der größe nach sortiert hinein schreibe???


DeCodeGuru - Sa 23.11.02 15:28

ein netter kleiner Sotieralgo ist Bubble-Sort. Der ist zwar etwas langsam, aber dafür ist er auch einfach. Hier ist etwas Code (recht alt, deshalb verzeiht mir den etwas "doofen Code"):


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:
unit Main;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    testsort: array [0..19] of Integer;
    procedure DoSort;
    procedure DoShow;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DoSort;
var
  i, zw, ok: Integer;
begin
  repeat
    ok := 1;
    for i := 0 to 19 do
    begin
      if testsort[i] > testsort [i + 1] then
      begin
        zw := testsort [i];
        testsort[i] := testsort[i + 1];
        testsort[i + 1] := zw;
        ok := 0;                        
      end;
    end;
    DoShow;
  until ok = 1;
  DoShow;
end;

procedure TForm1.DoShow;
var
  i: Integer;
  tmp: String;
begin
  for i := 0 to 19 do
  begin
    if testsort[i] > 9 then
      tmp := tmp + '   '+IntToStr(testsort[i])
    else
      tmp := tmp + '     '+IntToStr(testsort[i]);
  end;
  Memo1.Lines.Add(tmp);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  testsort[0] :=  19;
  testsort[1] :=  11;
  testsort[2] :=  18;
  testsort[3] :=  17;
  testsort[4] :=   5;
  testsort[5] :=   1;
  testsort[6] :=   9;
  testsort[7] :=  10;
  testsort[8] :=   6;
  testsort[9] :=   8;
  testsort[10] :=  2;
  testsort[11] := 16;
  testsort[12] := 15;
  testsort[13] := 12;
  testsort[14] :=  4;
  testsort[15] :=  7;
  testsort[16] :=  3;
  testsort[17] := 14;
  testsort[18] := 13;
  testsort[19] :=  0;
  DoShow;
  Application.ProcessMessages;
  DoSort;
end;


Kannst es ja mal probieren :wink:


Christian S. - Sa 23.11.02 21:37

Hi!

Hier [http://asg.region-kaiserslautern.de/_infoSCHUL/_5_informatik_sort_web/_sortierverfahren_delphi/index_inf_sort.html] gibt es eine Zusammenstellung der beliebtesten Sortieralgorithmen.

Aber DeCodeGuru (der mit dem doofen Code :wink: ) hat schon recht: bei zehn Zahlen reicht Bubblesort völlig aus.

MfG,
Peter


LCS - So 24.11.02 15:26

Und hier [http://lcschuhmann.de/downloads/sortdemo.rar] auch noch mal, fix und fertig mit Demoprogramm.

Gruss Lothar