AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Sortierung unwirksam

Ein Thema von Neutral General · begonnen am 13. Sep 2007 · letzter Beitrag vom 15. Sep 2007
 
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

Sortierung unwirksam

  Alt 13. Sep 2007, 13:03
Hi,

Ich hab eine Liste abgeleitet von TList mit Items die je zwei Integerwerte enthalten.
Ich hab mal was vereinfachtes nachprogrammiert. Angenommen wir haben eine TPointList mit Items vom Typ PPoint. Jetzt will ich diese Liste nach den y-Werten der Punkte sortieren. Dazu benutze ich Quicksort.
Hier erstmal der Quelltext:

Delphi-Quellcode:
interface

type
  PPoint = ^TPoint;

  TPointList = class(TList)
  private
    function GetItem(Index: Integer): PPoint;
    procedure SetItem(Index: Integer; const Value: PPoint)
  public
    function Add(APoint: PPoint): Integer;
    property Items[Index: Integer]: PPoint read GetItem write SetItem; default;
  end;
Implementation der Liste:

Delphi-Quellcode:
{ TPointList }

function TPointList.Add(APoint: PPoint): Integer;
begin
  Result := inherited Add(APoint);
end;

function TPointList.GetItem(Index: Integer): PPoint;
begin
  Result := PPoint(inherited Items[Index]);
end;

procedure TPointList.SetItem(Index: Integer; const Value: PPoint);
begin
  inherited Items[Index] := Value;
end;
Eigentlicher Quellcode:

Delphi-Quellcode:
var list: TPointList;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
  tmp: PPoint;
begin
  List := TPointList.Create;
  for i := 1 to 30 do // Liste mit zufälligen Punkten füllen
  begin
    New(tmp);
    tmp^.X := random(256);
    tmp^.Y := random(256);
    List.Add(tmp);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  // Unsortiert anzeigen
  for i:=0 to List.Count-1 do
    Listbox1.Items.Add('(' + IntToStr(List[i]^.X) + '|' + IntToStr(List[i]^.Y) + ')');
  // Sortieren
  SortList(list,0,List.Count-1);
  // Sortiert anzeigen
  for I := 0 to List.Count - 1 do
    Listbox2.Items.Add('(' + IntToStr(List[i]^.X) + '|' + IntToStr(List[i]^.Y) + ')');
end;

// Quicksort
procedure SortList(var AList: TPointlist; l,r: Integer);

  function Divide(s,t: Integer): Integer;
  var i,j: Integer;
      pivot: Integer;
      tmp: PPoint;
  begin
    i := s-1;
    j := t+1;
    pivot := AList[t]^.Y;
    repeat
      repeat
        inc(i)
      until AList[i]^.Y >= pivot;

      repeat
        dec(j);
      until AList[j]^.y <= pivot;

      tmp := AList[i];
      AList[i] := AList[j];
      AList[j] := tmp;
    until i <= j;
    tmp := AList[i];
    AList[i] := AList[j];
    AList[j] := tmp;

    Result := i;
  end;

var m: Integer;
begin
  if l < r then
  begin
    m := Divide(l,r);
    SortList(AList,l,m-1);
    SortList(AList,m+1,r);
  end;
end;
Dummerweise stehn in beiden Listboxen das gleiche. Die Liste wird einfach nicht sortiert. Habe mal mit Breakpoints in der Quicksort procedure geguckt und da wird alles Ordnungsgemäß ausgetauscht. Ach ja: Es ist wichtig das die Pointer ausgetauscht werden und nicht nur die Werte selbst!

Ich versteh jetzt nicht was da schief läuft...

Gruß
Neutral General
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:24 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz