class function FloorLog2(n: Integer): Integer; static;
class procedure Swap<T>(var values: array of T; left, right: Integer); static;
- class procedure SortThreeItems<T>(var values: array of T; const comparer: IComparer<T>; lo, mid, hi: Integer); static;
+ class procedure SortThreeItems<T>(var values: array of T; const comparer: IComparer<T>; left, mid, right: Integer); static;
- class procedure InsertionSort<T>(var values: array of T; const comparer: IComparer<T>; lo, hi: Integer); static;
+ class procedure InsertionSort<T>(var values: array of T; const comparer: IComparer<T>; left, right: Integer); static;
- class procedure DownHeap<T>(var values: array of T; const comparer: IComparer<T>; i, n, lo: Integer); static;
- class procedure HeapSort<T>(var values: array of T; const comparer: IComparer<T>; lo, hi: Integer); static;
+ class procedure DownHeap<T>(var values: array of T; const comparer: IComparer<T>; left, count, i: Integer); static;
+ class procedure HeapSort<T>(var values: array of T; const comparer: IComparer<T>; left, right: Integer); static;
- class function QuickSortPartition<T>(var values: array of T; const comparer: IComparer<T>; lo, hi: Integer): Integer; static;
+ class function QuickSortPartition<T>(var values: array of T; const comparer: IComparer<T>; left, right: Integer): Integer; static;
- class procedure IntroSort<T>(var values: array of T; const comparer: IComparer<T>; lo, hi, depthLimit: Integer); static;
- class procedure IntrospectiveSort<T>(var values: array of T; const comparer: IComparer<T>; left, count: Integer); static;
+ class procedure IntroSort<T>(var values: array of T; const comparer: IComparer<T>; left, right, depthLimit: Integer); overload; static;
+ class procedure IntroSort<T>(var values: array of T; const comparer: IComparer<T>; index, count: Integer); overload; static;
class procedure Sort<T>(var values: array of T); overload; static;
class procedure Sort<T>(var values: array of T; const comparer: IComparer<T>); overload; static;
class procedure TArray.SortThreeItems<T>(var values: array of T;
- const comparer: IComparer<T>; lo, mid, hi: Integer);
+ const comparer: IComparer<T>; left, mid, right: Integer);
- if comparer.Compare(values[lo], values[mid]) > 0 then
+ if comparer.Compare(values[left], values[mid]) > 0 then
- values[lo] := values[mid];
+ values[left] := values[mid];
- if comparer.Compare(values[lo], values[hi]) > 0 then
+ if comparer.Compare(values[left], values[right]) > 0 then
- values[lo] := values[hi];
+ values[left] := values[right];
- if comparer.Compare(values[mid], values[hi]) > 0 then
+ if comparer.Compare(values[mid], values[right]) > 0 then
- values[mid] := values[hi];
+ values[mid] := values[right];
class procedure TArray.DownHeap<T>(var values: array of T;
- const comparer: IComparer<T>; i, n, lo: Integer);
+ const comparer: IComparer<T>; left, count, i: Integer);
- temp := values[lo + i - 1];
+ temp := values[left + i - 1];
- if (child < n) and (comparer.Compare(values[lo + child - 1], values[lo + child]) < 0) then
+ if (child < count) and (comparer.Compare(values[left + child - 1], values[left + child]) < 0) then
- if not comparer.Compare(temp, values[lo + child - 1]) < 0 then
+ if not comparer.Compare(temp, values[left + child - 1]) < 0 then
- values[lo + i - 1] := values[lo + child - 1];
+ values[left + i - 1] := values[left + child - 1];
- values[lo + i - 1] := temp;
+ values[left + i - 1] := temp;
class procedure TArray.HeapSort<T>(var values: array of T;
- const comparer: IComparer<T>; lo, hi: Integer);
+ const comparer: IComparer<T>; left, right: Integer);
- for i := n div 2 downto 1 do
- DownHeap<T>(values, comparer, i, n, lo);
+ count := right - left + 1;
+ for i := count div 2 downto 1 do
+ DownHeap<T>(values, comparer, left, count, i);
+ for i := count downto 2 do
- Swap<T>(values, lo, lo + i - 1);
- DownHeap<T>(values, comparer, 1, i - 1, lo);
+ Swap<T>(values, left, left + i - 1);
+ DownHeap<T>(values, comparer, left, i - 1, 1);
class procedure TArray.InsertionSort<T>(var values: array of T;
- const comparer: IComparer<T>; lo, hi: Integer);
+ const comparer: IComparer<T>; left, right: Integer);
- for i := lo to hi - 1 do
+ for i := left + 1 to right do
- while (j >= lo) and (comparer.Compare(temp, values[j]) < 0) do
+ while (j > left) and (comparer.Compare(temp, values[j - 1]) < 0) do
- values[j + 1] := values[j];
+ values[j] := values[j - 1];
class function TArray.QuickSortPartition<T>(var values: array of T;
- const comparer: IComparer<T>; lo, hi: Integer): Integer;
+ const comparer: IComparer<T>; left, right: Integer): Integer;
- mid, left, right: Integer;
+ mid, pivotIndex: Integer;
- mid := lo + (hi - lo) div 2;
+ mid := left + (right - left) div 2;
- SortThreeItems<T>(values, comparer, lo, mid, hi);
+ SortThreeItems<T>(values, comparer, left, mid, right);
values[mid] := values[right];
- values[left] := values[hi - 1];
- values[hi - 1] := pivot;
+ values[left] := values[pivotIndex];
+ values[pivotIndex] := pivot;
class procedure TArray.IntroSort<T>(var values: array of T;
- const comparer: IComparer<T>; lo, hi, depthLimit: Integer);
+ const comparer: IComparer<T>; left, right, depthLimit: Integer);
- length, pivot: Integer;
- if length <= IntrosortSizeThreshold then
+ count := right - left + 1;
+ if count <= IntrosortSizeThreshold then
- if comparer.Compare(values[lo], values[hi]) > 0 then
- Swap<T>(values, lo, hi);
+ if comparer.Compare(values[left], values[right]) > 0 then
+ Swap<T>(values, left, right);
- SortThreeItems<T>(values, comparer, lo, hi - 1, hi);
+ SortThreeItems<T>(values, comparer, left, right - 1, right);
- InsertionSort<T>(values, comparer, lo, hi);
+ InsertionSort<T>(values, comparer, left, right);
- HeapSort<T>(values, comparer, lo, hi);
+ HeapSort<T>(values, comparer, left, right);
- pivot := QuickSortPartition<T>(values, comparer, lo, hi);
- IntroSort<T>(values, comparer, pivot + 1, hi, depthLimit);
+ pivot := QuickSortPartition<T>(values, comparer, left, right);
+ IntroSort<T>(values, comparer, pivot + 1, right, depthLimit);
-class procedure TArray.IntrospectiveSort<T>(var values: array of T;
- const comparer: IComparer<T>; left, count: Integer);
+class procedure TArray.IntroSort<T>(var values: array of T;
+ const comparer: IComparer<T>; index, count: Integer);
- IntroSort<T>(values, comparer, left, count + left - 1, 2 * FloorLog2(Length(values)));
+ IntroSort<T>(values, comparer, index, count + index - 1, 2 * FloorLog2(count));
class procedure TArray.Sort<T>(var values: array of T);
- IntrospectiveSort<T>(values, TComparer<T>.Default, Low(Values), High(Values));
+ IntroSort<T>(values, TComparer<T>.Default, Low(Values), High(Values));
class procedure TArray.Sort<T>(var values: array of T;
const comparer: IComparer<T>);
- IntrospectiveSort<T>(values, comparer, Low(Values), High(Values));
+ IntroSort<T>(values, comparer, Low(Values), High(Values));
class procedure TArray.Sort<T>(var values: array of T;
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
- IntrospectiveSort<T>(values, comparer, index, count);
+ IntroSort<T>(values, comparer, index, count);