Пример #1
0
        private bool SwapOut(T[] entries)
        {
            // if (Median.CompareTo(entries[Left]) == 0) Swap(entries, LeftMedian++, Left);
            var leftCompare = Median.AbstractCompareTo(entries[Left]);

            if (!leftCompare.ComparisonSucceeded)
            {
                return(false);
            }
            if (leftCompare.ComparisonResult == 0)
            {
                Swap(entries, LeftMedian++, Left);
            }

            // if (Median.CompareTo(entries[Right]) == 0) Swap(entries, Right, RightMedian--);
            var rightCompare = Median.AbstractCompareTo(entries[Right]);

            if (!rightCompare.ComparisonSucceeded)
            {
                return(false);
            }
            if (rightCompare.ComparisonResult == 0)
            {
                Swap(entries, Right, RightMedian--);
            }
            return(true);
        }
Пример #2
0
        private SortResult <T> Partition(T[] entries)
        {
            var first = Left;
            var last  = Right;

            LeftMedian  = first;
            RightMedian = last;
            while (true)
            {
                //[Assert]There exists some index >= Left where entries[index] >= Median
                //[Assert]There exists some index <= Right where entries[index] <= Median
                // So, there is no need for Left or Right bound checks

                // while (Median.CompareTo(entries[Left]) > 0) Left++;
                while (true)
                {
                    var compareResult = Median.AbstractCompareTo(entries[Left]);
                    if (!compareResult.ComparisonSucceeded)
                    {
                        return(new SortResult <T>(entries, entries[Left], Median));
                    }
                    if (compareResult.ComparisonResult > 0)
                    {
                        Left++;
                    }
                    else
                    {
                        break;
                    }
                }

                //while (Median.CompareTo(entries[Right]) < 0) Right--;
                while (true)
                {
                    var compareResult = Median.AbstractCompareTo(entries[Right]);
                    if (!compareResult.ComparisonSucceeded)
                    {
                        return(new SortResult <T>(entries, entries[Right], Median));
                    }
                    if (compareResult.ComparisonResult < 0)
                    {
                        Right--;
                    }
                    else
                    {
                        break;
                    }
                }

                //[Assert]entries[Right] <= Median <= entries[Left]
                if (Right <= Left)
                {
                    break;
                }

                Swap(entries, Left, Right);
                if (!SwapOut(entries))
                {
                    return(new SortResult <T>(entries, entries[Left], entries[Right]));
                }
                Left++;
                Right--;
                //[Assert]entries[first:Left - 1] <= Median <= entries[Right + 1:last]
            }

            if (Left == Right)
            {
                Left++;
                Right--;
            }
            //[Assert]Right < Left
            SwapIn(entries, first, last);

            //[Assert]entries[first:Right] <= Median <= entries[Left:last]
            //[Assert]entries[Right + 1:Left - 1] == Median when non-empty

            return(new SortResult <T>(entries));
        }