Browse Source

* select the middle element in the default quicksort implementation in a way
that doesn't generate arithmetic overflow for very large arrays

git-svn-id: trunk@41241 -

nickysn 6 years ago
parent
commit
00a67caa40
1 changed files with 4 additions and 4 deletions
  1. 4 4
      rtl/inc/sortbase.pp

+ 4 - 4
rtl/inc/sortbase.pp

@@ -94,7 +94,7 @@ begin
  repeat
    I := L;
    J := R;
-   PivotIdx := (L + R) div 2;
+   PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
    P := ItemPtrs[PivotIdx];
    repeat
      while (I < PivotIdx) and (Comparer(P, ItemPtrs[i]) > 0) do
@@ -161,7 +161,7 @@ procedure QuickSort_PtrList_Context(ItemPtrs: PPointer; ItemCount: SizeUInt; Com
     repeat
       I := L;
       J := R;
-      PivotIdx := (L + R) div 2;
+      PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
       P := ItemPtrs[PivotIdx];
       repeat
         while (I < PivotIdx) and (Comparer(P, ItemPtrs[I], Context) > 0) do
@@ -230,7 +230,7 @@ var
     repeat
       I := L;
       J := R;
-      PivotIdx := (L + R) div 2;
+      PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
       P := Items + ItemSize*PivotIdx;
       repeat
         while (I < PivotIdx) and (Comparer(P, Items + ItemSize*I, Context) > 0) do
@@ -308,7 +308,7 @@ procedure QuickSort_ItemList_CustomItemExchanger_Context(
     repeat
       I := L;
       J := R;
-      PivotIdx := (L + R) div 2;
+      PivotIdx := L + ((R - L) shr 1); { same as ((L + R) div 2), but without the possibility of overflow }
       P := Items + ItemSize*PivotIdx;
       repeat
         while (I < PivotIdx) and (Comparer(P, Items + ItemSize*I, Context) > 0) do