示例#1
0
        internal static int BasicPartition(SwappableData data, int begin, int end)
        {
            var pivot      = data[begin];
            var rightBegin = begin + 1;

            for (var i = rightBegin; i <= end; i++)
            {
                if (data[i] < pivot)
                {
                    data.Swap(i, rightBegin);
                    rightBegin++;
                }
            }

            var mid = rightBegin - 1;

            data.Swap(begin, mid);
            return(mid);
        }
示例#2
0
        internal static int DoublePointerPartition(SwappableData data, int begin, int end)
        {
            var mid   = (int)((begin + end) / 2);
            var pivot = MedianOfThree(data[begin], data[mid], data[end]);
            var i     = begin - 1;
            var j     = end + 1;

            while (true)
            {
                while (true)
                {
                    i++;
                    if (data[i] >= pivot)
                    {
                        break;
                    }
                }
                while (true)
                {
                    j--;
                    if (data[j] <= pivot)
                    {
                        break;
                    }
                }
                if (i < j)
                {
                    if (data[i] != data[j])
                    {
                        data.Swap(i, j);
                    }
                }
                else
                {
                    return(j);
                }
            }
        }