diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs index c0da6435370..b1253aa3ff2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs @@ -141,45 +141,42 @@ namespace System.Collections.Generic Debug.Assert(depthLimit >= 0); Debug.Assert(comparer != null); - int hi = keys.Length - 1; - while (hi > 0) + int partitionSize = keys.Length; + while (partitionSize > 1) { - int partitionSize = hi + 1; - if (partitionSize <= Array.IntrosortSizeThreshold) { - Debug.Assert(partitionSize >= 2); if (partitionSize == 2) { - SwapIfGreater(keys, comparer, 0, hi); + SwapIfGreater(keys, comparer, 0, 1); return; } if (partitionSize == 3) { - SwapIfGreater(keys, comparer, 0, hi - 1); - SwapIfGreater(keys, comparer, 0, hi); - SwapIfGreater(keys, comparer, hi - 1, hi); + SwapIfGreater(keys, comparer, 0, 1); + SwapIfGreater(keys, comparer, 0, 2); + SwapIfGreater(keys, comparer, 1, 2); return; } - InsertionSort(keys.Slice(0, hi+1), comparer); + InsertionSort(keys.Slice(0, partitionSize), comparer); return; } if (depthLimit == 0) { - HeapSort(keys.Slice(0, hi+1), comparer); + HeapSort(keys.Slice(0, partitionSize), comparer); return; } depthLimit--; - int p = PickPivotAndPartition(keys.Slice(0, hi+1), comparer); + int p = PickPivotAndPartition(keys.Slice(0, partitionSize), comparer); // Note we've already partitioned around the pivot and do not have to move the pivot again. - IntroSort(keys[(p+1)..(hi+1)], depthLimit, comparer); - hi = p - 1; + IntroSort(keys[(p+1)..partitionSize], depthLimit, comparer); + partitionSize = p; } } @@ -404,14 +401,11 @@ namespace System.Collections.Generic Debug.Assert(!keys.IsEmpty); Debug.Assert(depthLimit >= 0); - int hi = keys.Length - 1; - while (hi > 0) + int partitionSize = keys.Length; + while (partitionSize > 1) { - int partitionSize = hi + 1; - if (partitionSize <= Array.IntrosortSizeThreshold) { - Debug.Assert(partitionSize >= 2); if (partitionSize == 2) { @@ -446,7 +440,7 @@ namespace System.Collections.Generic // Note we've already partitioned around the pivot and do not have to move the pivot again. IntroSort(keys[(p+1)..partitionSize], depthLimit); - hi = p - 1; + partitionSize = p; } } @@ -632,26 +626,23 @@ namespace System.Collections.Generic Debug.Assert(depthLimit >= 0); Debug.Assert(comparer != null); - int hi = keys.Length - 1; - while (hi > 0) + int partitionSize = keys.Length; + while (partitionSize > 1) { - int partitionSize = hi + 1; - if (partitionSize <= Array.IntrosortSizeThreshold) { - Debug.Assert(partitionSize >= 2); if (partitionSize == 2) { - SwapIfGreaterWithValues(keys, values, comparer, 0, hi); + SwapIfGreaterWithValues(keys, values, comparer, 0, 1); return; } if (partitionSize == 3) { - SwapIfGreaterWithValues(keys, values, comparer, 0, hi - 1); - SwapIfGreaterWithValues(keys, values, comparer, 0, hi); - SwapIfGreaterWithValues(keys, values, comparer, hi - 1, hi); + SwapIfGreaterWithValues(keys, values, comparer, 0, 1); + SwapIfGreaterWithValues(keys, values, comparer, 0, 2); + SwapIfGreaterWithValues(keys, values, comparer, 1, 2); return; } @@ -670,7 +661,7 @@ namespace System.Collections.Generic // Note we've already partitioned around the pivot and do not have to move the pivot again. IntroSort(keys[(p+1)..partitionSize], values[(p+1)..partitionSize], depthLimit, comparer); - hi = p - 1; + partitionSize = p; } } @@ -856,26 +847,23 @@ namespace System.Collections.Generic Debug.Assert(values.Length == keys.Length); Debug.Assert(depthLimit >= 0); - int hi = keys.Length - 1; - while (hi > 0) + int partitionSize = keys.Length; + while (partitionSize > 1) { - int partitionSize = hi + 1; - if (partitionSize <= Array.IntrosortSizeThreshold) { - Debug.Assert(partitionSize >= 2); if (partitionSize == 2) { - SwapIfGreaterWithValues(keys, values, 0, hi); + SwapIfGreaterWithValues(keys, values, 0, 1); return; } if (partitionSize == 3) { - SwapIfGreaterWithValues(keys, values, 0, hi - 1); - SwapIfGreaterWithValues(keys, values, 0, hi); - SwapIfGreaterWithValues(keys, values, hi - 1, hi); + SwapIfGreaterWithValues(keys, values, 0, 1); + SwapIfGreaterWithValues(keys, values, 0, 2); + SwapIfGreaterWithValues(keys, values, 1, 2); return; } @@ -894,7 +882,7 @@ namespace System.Collections.Generic // Note we've already partitioned around the pivot and do not have to move the pivot again. IntroSort(keys[(p+1)..partitionSize], values[(p+1)..partitionSize], depthLimit); - hi = p - 1; + partitionSize = p; } }