Ver código fonte

* fixed bug in QuickSort_ItemList_CustomItemExchanger_Context and
QuickSort_ItemList_Context and which can cause wrong sort results, due to not
taking into account that the pivot can be moved by the swap operation

git-svn-id: trunk@41191 -

nickysn 6 anos atrás
pai
commit
ea340b9481
1 arquivos alterados com 18 adições e 4 exclusões
  1. 18 4
      rtl/inc/sortbase.pp

+ 18 - 4
rtl/inc/sortbase.pp

@@ -183,9 +183,16 @@ var
           Dec(J);
           Dec(J);
         If I <= J then
         If I <= J then
         begin
         begin
-          Move((Items + ItemSize*I)^, TempBuf^, ItemSize);
-          Move((Items + ItemSize*J)^, (Items + ItemSize*I)^, ItemSize);
-          Move(TempBuf^, (Items + ItemSize*J)^, ItemSize);
+          if I < J then
+          begin
+            Move((Items + ItemSize*I)^, TempBuf^, ItemSize);
+            Move((Items + ItemSize*J)^, (Items + ItemSize*I)^, ItemSize);
+            Move(TempBuf^, (Items + ItemSize*J)^, ItemSize);
+            if P = (Items + ItemSize*I) then
+              P := Items + ItemSize*J
+            else if P = (Items + ItemSize*J) then
+              P := Items + ItemSize*I;
+          end;
           Inc(I);
           Inc(I);
           Dec(J);
           Dec(J);
         end;
         end;
@@ -239,7 +246,14 @@ procedure QuickSort_ItemList_CustomItemExchanger_Context(
           Dec(J);
           Dec(J);
         If I <= J then
         If I <= J then
         begin
         begin
-          Exchanger(Items + ItemSize*I, Items + ItemSize*J, Context);
+          if I < J then
+          begin
+            Exchanger(Items + ItemSize*I, Items + ItemSize*J, Context);
+            if P = (Items + ItemSize*I) then
+              P := Items + ItemSize*J
+            else if P = (Items + ItemSize*J) then
+              P := Items + ItemSize*I;
+          end;
           Inc(I);
           Inc(I);
           Dec(J);
           Dec(J);
         end;
         end;