예제 #1
0
 public void Sort(T[] keys, int index, int length, IComparer <T> comparer)
 {
     try
     {
         if (comparer == null || comparer == Comparer <T> .Default)
         {
             if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
             {
                 GenericArraySortHelper <T> .IntrospectiveSort(keys, index, length);
             }
             else
             {
                 GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, index, length + index - 1, 32);
             }
         }
         else if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
         {
             ArraySortHelper <T> .IntrospectiveSort(keys, index, length, comparer);
         }
         else
         {
             ArraySortHelper <T> .DepthLimitedQuickSort(keys, index, length + index - 1, comparer, 32);
         }
     }
     catch (IndexOutOfRangeException ex)
     {
         IntrospectiveSortUtilities.ThrowOrIgnoreBadComparer((object)comparer);
     }
     catch (Exception ex)
     {
         throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_IComparerFailed"), ex);
     }
 }
예제 #2
0
        private static void DepthLimitedQuickSort(T[] keys, int left, int right, int depthLimit)
        {
            while (depthLimit != 0)
            {
                int index1 = left;
                int index2 = right;
                int index3 = index1 + (index2 - index1 >> 1);
                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index1, index3);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index1, index2);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, index3, index2);

                T obj1 = keys[index3];
                do
                {
                    if ((object)obj1 == null)
                    {
                        while ((object)keys[index2] != null)
                        {
                            --index2;
                        }
                    }
                    else
                    {
                        while (obj1.CompareTo(keys[index1]) > 0)
                        {
                            ++index1;
                        }
                        while (obj1.CompareTo(keys[index2]) < 0)
                        {
                            --index2;
                        }
                    }
                    if (index1 <= index2)
                    {
                        if (index1 < index2)
                        {
                            T obj2 = keys[index1];
                            keys[index1] = keys[index2];
                            keys[index2] = obj2;
                        }
                        ++index1;
                        --index2;
                    }
                    else
                    {
                        break;
                    }
                }while (index1 <= index2);
                --depthLimit;
                if (index2 - left <= right - index1)
                {
                    if (left < index2)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, left, index2, depthLimit);
                    }
                    left = index1;
                }
                else
                {
                    if (index1 < right)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, index1, right, depthLimit);
                    }
                    right = index2;
                }
                if (left >= right)
                {
                    return;
                }
            }
            GenericArraySortHelper <T> .Heapsort(keys, left, right);
        }
        // Token: 0x06003A2D RID: 14893 RVA: 0x000DC89C File Offset: 0x000DAA9C
        private static void DepthLimitedQuickSort(T[] keys, int left, int right, int depthLimit)
        {
            while (depthLimit != 0)
            {
                int num  = left;
                int num2 = right;
                int num3 = num + (num2 - num >> 1);
                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num, num3);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num, num2);

                GenericArraySortHelper <T> .SwapIfGreaterWithItems(keys, num3, num2);

                T t = keys[num3];
                do
                {
                    if (t == null)
                    {
                        while (keys[num2] != null)
                        {
                            num2--;
                        }
                    }
                    else
                    {
                        while (t.CompareTo(keys[num]) > 0)
                        {
                            num++;
                        }
                        while (t.CompareTo(keys[num2]) < 0)
                        {
                            num2--;
                        }
                    }
                    if (num > num2)
                    {
                        break;
                    }
                    if (num < num2)
                    {
                        T t2 = keys[num];
                        keys[num]  = keys[num2];
                        keys[num2] = t2;
                    }
                    num++;
                    num2--;
                }while (num <= num2);
                depthLimit--;
                if (num2 - left <= right - num)
                {
                    if (left < num2)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, left, num2, depthLimit);
                    }
                    left = num;
                }
                else
                {
                    if (num < right)
                    {
                        GenericArraySortHelper <T> .DepthLimitedQuickSort(keys, num, right, depthLimit);
                    }
                    right = num2;
                }
                if (left >= right)
                {
                    return;
                }
            }
            GenericArraySortHelper <T> .Heapsort(keys, left, right);
        }