Advertisement
Guest User

Untitled

a guest
Sep 7th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.93 KB | None | 0 0
  1. Procedure QuickSort(FList: PPointerList; L, R : Longint;
  2.                      Compare: TListSortCompare);
  3. var
  4.   I, J : Longint;
  5.   P, Q : Pointer;
  6. begin
  7.  repeat
  8.    I := L;
  9.    J := R;
  10.    P := FList^[ (L + R) div 2 ];
  11.    repeat
  12.      while Compare(P, FList^[i]) > 0 do
  13.        I := I + 1;
  14.      while Compare(P, FList^[J]) < 0 do
  15.        J := J - 1;
  16.      If I <= J then
  17.      begin
  18.        Q := FList^[I];
  19.        Flist^[I] := FList^[J];
  20.        FList^[J] := Q;
  21.        I := I + 1;
  22.        J := J - 1;
  23.      end;
  24.    until I > J;
  25.    // sort the smaller range recursively
  26.    // sort the bigger range via the loop
  27.    // Reasons: memory usage is O(log(n)) instead of O(n) and loop is faster than recursion
  28.    if J - L < R - I then
  29.    begin
  30.      if L < J then
  31.        QuickSort(FList, L, J, Compare);
  32.      L := I;
  33.    end
  34.    else
  35.    begin
  36.      if I < R then
  37.        QuickSort(FList, I, R, Compare);
  38.      R := J;
  39.    end;
  40.  until L >= R;
  41. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement