Thema: InsertSort

Einzelnen Beitrag anzeigen

Bjoerk

Registriert seit: 28. Feb 2011
Ort: Mannheim
1.384 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: InsertSort

  Alt 18. Mai 2012, 12:48
BusyByte, meine Posts sind grundsätzlich nie böse oder persönlich gemeint. Du benutzt new ohne dispose, das produziert halt Speicherlecks. Des weitern ist der Code gelinde gesagt ziemlich unstrukturiert und trennt auch nicht Logik von Darstellung und hat auch mit der Frage des TE nichts zu tun. Dein Hinweis, hier TList statt ein Array zu benutzen ist fast schon aberwitzig, benutzt TList intern doch ein statisches Array. Die Frage bezog sich auch den Insertsort, TList benutzt den Quicksort. Anyway, nicht für ungut.

Da du mir vorwirfst, hier nur herumnörgeln und sonst nichts zum Besten geben, hier mein Vorschlag. Wie gesagt, hat mir der Frage des TE nicht zu tun, exklusiv für dich:

Delphi-Quellcode:
unit AUnit;

interface

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

type
  PRecordTyp = ^TRecordTyp;

  TRecordTyp = record
    I1: integer;
    S1: String;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function CompareByI1(const Item1, Item2: PRecordTyp): integer;
begin
  Result := 0;
  if Item1^.I1 > Item2^.I1 then
    Result := 1
  else
    if Item1^.I1 < Item2^.I1 then
      Result := -1
end;

function CompareByS1(const Item1, Item2: PRecordTyp): integer;
begin
  Result := 0;
  if Item1^.S1 > Item2^.S1 then
    Result := 1
  else
    if Item1^.S1 < Item2^.S1 then
      Result := -1
end;

procedure AddItem(const List: TList; const I1: integer; const S1: string);
var
  P: PRecordTyp;
begin
  P := New(PRecordTyp);
  P.I1 := I1;
  P.S1 := S1;
  List.Add(P);
end;

procedure DelItem(const List: TList; const Index: integer);
var
  P: PRecordTyp;
begin
  P := List.Items[Index];
  Dispose(P);
  List.Delete(Index);
end;

procedure FillList(const List: Tlist);
const
  N = 10000;
var
  I, I1: integer;
  S1: string;
begin
  for I := 1 to N do
  begin
    I1 := Random(N);
    S1 := IntToStr(Random(N));
    AddItem(List, I1, S1);
  end;
end;

procedure FillListBox(const List: Tlist; const ListBox: TListBox);
var
  I: integer;
  P: PRecordTyp;
begin
  ListBox.Items.BeginUpdate;
  ListBox.Clear;
  try
    for I := 0 to List.Count - 1 do
    begin
      P := List[I];
      ListBox.Items.Add(IntToStr(P^.I1) + ' - ' + P^.S1);
    end;
  finally
    ListBox.Items.EndUpdate;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  List: TList;
begin
  List := TList.Create;
  try
    FillList(List);
    List.Sort(@CompareByI1);
    FillListBox(List, ListBox1);
    while List.Count > 0 do
      DelItem(List, List.Count - 1);
  finally
    List.Free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  List: TList;
begin
  List := TList.Create;
  try
    FillList(List);
    List.Sort(@CompareByS1);
    FillListBox(List, ListBox1);
    while List.Count > 0 do
      DelItem(List, List.Count - 1);
  finally
    List.Free;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Randomize;
end;

end.
  Mit Zitat antworten Zitat