Einzelnen Beitrag anzeigen

Benutzerbild von cherry
cherry

Registriert seit: 14. Nov 2005
561 Beiträge
 
RAD-Studio 2009 Ent
 
#5

AW: Array of Array of string Quicksort

  Alt 8. Nov 2010, 08:01
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;
Ist das nur mein Gefühl, oder ist die ganze Welt verrückt geworden!?
  Mit Zitat antworten Zitat