unit heapsort_verfahren_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
Theapsort = class(TForm)
mAusgabe: TMemo;
btnZahlen: TButton;
btnSortieren: TButton;
procedure btnZahlenClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnSortierenClick(Sender: TObject);
procedure refillMemo();
private
i: integer;
Data: array of integer;
procedure sortierverfahren(Data: array of integer);
procedure Erstelleheap(i: integer; Data: array of integer; knot: integer);
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
heapsort: Theapsort;
implementation
{$R *.dfm}
procedure Theapsort.FormCreate(Sender: TObject);
begin
SetLength(Data,1);
end;
procedure Theapsort.btnSortierenClick(Sender: TObject);
begin
sortierverfahren(Data);
refillMemo();
end;
procedure Theapsort.btnZahlenClick(Sender: TObject);
var
c, zahl,i: integer;
begin
for c:=0 to 10 do
begin
randomize;
zahl:=random(100)+1;
SetLength(Data, Length(Data)+1);
i:=i+1;
Data[i]:=zahl;
refillMemo();
end;
end;
procedure Theapsort.sortierverfahren(Data: array of integer);
var
knot, temp, i: integer;
begin
i:= Length(Data);
knot:= i div 2;
while knot > 0 do
begin
dec(knot);
Erstelleheap(i, Data, knot);
end;
while i >= 0 do
begin
temp:= Data[0];
Data[0]:= Data[i];
Data[i]:= temp;
Erstelleheap(i,Data, 0);
Dec(i);
end;
end;
procedure Theapsort.Erstelleheap(i: integer; Data: array of integer; knot: integer);
var
temp_knot_value, subknot: integer;
begin
temp_knot_value := Data[knot];
while knot < i div 2 do
begin
subknot := 2*knot+1;
if (subknot < i-1) and (Data[subknot]< Data [subknot +1])
then Inc(subknot);
if temp_knot_value >= Data[subknot]
then break;
Data[knot]:= Data[subknot];
end;
Data[knot]:= temp_knot_value;
end;
procedure Theapsort.refillMemo();
var
a:Integer; Data: Array of integer;
begin
mAusgabe.Text:='';
for a:= 1 to Length(Data)-1 do
begin
mAusgabe.text:= mAusgabe.Text + Inttostr(Data[a])+#13#10;
end;
end;
end.