Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Array of Array of string Quicksort (https://www.delphipraxis.net/146029-array-array-string-quicksort.html)

cherry 12. Jan 2010 14:16


Array of Array of string Quicksort
 
Hallo Zusammen...

Ich suche verzweifelt nach einer Funktion die ein Array of Array of string Alphabetisch sortiert. Und zwar möchte ich angeben können nach welcher Spalte.
Ein aufruf könnte dann z.b. so aussehen.

Delphi-Quellcode:
type
  TStringArray2 = array of array of string;

var
  MyArr: TStringArray2;

  Quicksort(MyArr, 0, Length(MyArr), 2);
Ich hab HIER! etwas gefunden, leider bringe ich das nicht zum laufen. An der Stelle A[L]:=A[R]; gibts dann immer eine Zugriffsverletzung.

Danke.

Kornelius Ley 12. Jan 2010 14:45

Re: Array of Array of string Quicksort
 
Hallo cherry

length(MyArr) ist falsch, glaube ich, es muss length(MyArr)-1 heissen, weil der Index mit 0 beginnt

cherry 12. Jan 2010 14:57

Re: Array of Array of string Quicksort
 
LOOOOOOOOOOOOOOOOOOOOLLLLLLLLLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLL
thx

Keks 4. Nov 2010 17:08

AW: Array of Array of string Quicksort
 
Hi cherry,
"Array of Array of String alphabetisch sortieren" -> genau das brauche ich auch. Habe aber gerade meine Schwierigkeiten, die empfohlene Funktion anzupassen. Bei Dir scheints letztlich ja geklappt zu haben ... Kannst Du bitte Deinen Code posten?

cherry 8. Nov 2010 08:01

AW: Array of Array of string Quicksort
 
Delphi-Quellcode:
QuickSortStringArray2Ex(StringArrQuota,0,Length(StringArrQuota)-1,SortedCol);
Delphi-Quellcode:
{******************************************************************************}
// QUICK SORT STRING ARRAY 2 EXTENDED (multidimensional)
// Extended in this case: The values will be compared UPPERCASE, a < B
// http://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Quicksort#Delphi
{******************************************************************************}
procedure QuickSortStringArray2Ex(var A: TStringArray2; L, R: Integer; SortCol: Integer);
var
  OrigL,
  OrigR: Integer;
  Pivot: string;
  GoodPivot,
  SortPartitions: Boolean;
  tmp: TStringArray;
begin
  if L<R then begin
    Pivot:=UPPERCASE(A[L+Random(R-L),SortCol]);
    OrigL:=L; //saving original bounds
    OrigR:=R;
    repeat
      L:=OrigL; //restoring original bounds if we
      R:=OrigR; //have chosen a bad pivot value
      while L<R do begin
        while (L<R) and (UPPERCASE(A[L,SortCol])<Pivot) do Inc(L);
        while (L<R) and (UPPERCASE(A[R,SortCol])>=Pivot) do Dec(R);
        if (L<R) then begin
          tmp:=A[L];
          A[L]:=A[R];
          A[R]:=tmp;
          Dec(R);
          Inc(L);
        end;
      end;
      if UPPERCASE(A[L,SortCol])>=Pivot then Dec(L);                           //has we managed to choose
      GoodPivot:=L>=OrigL;                                                     //a good pivot value?
      SortPartitions:=True;                                                    //if so, then sort on
      if not GoodPivot then begin                                              //bad luck, the pivot is the smallest one in our range
        GoodPivot:=True;                                                       //let's presume that all the values are equal to pivot
        SortPartitions:=False;                                                 //then no need to sort it
        for R := OrigL to OrigR do if UPPERCASE(A[R,SortCol])<>Pivot then begin //we have at least one different value than our pivot
          Pivot:=UPPERCASE(A[R,SortCol]);                                      //so this will be our new pivot
          GoodPivot:=False;                                                    //we have to start again sorting this range
          Break;
        end;
      end;
    until GoodPivot;
    if SortPartitions then begin
      QuickSortStringArray2Ex(A, OrigL, L, SortCol);
      QuickSortStringArray2Ex(A, L+1, OrigR, SortCol);
    end;
  end;
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:58 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