コード例 #1
0
ファイル: HeapMASort.cs プロジェクト: madmazoku/OTUS-Algo
        static void SinkDown(IMemoryAcessor ma, long root, long start, long end, CancellationToken token)
        {
            long size = end - start;

            while (true)
            {
                long maxIndex   = root;
                long leftIndex  = (root << 1) + 1;
                long rightIndex = leftIndex + 1;
                if (leftIndex < size)
                {
                    if (ma.Read(start + maxIndex) < ma.Read(start + leftIndex))
                    {
                        maxIndex = leftIndex;
                    }
                    if (rightIndex < size && ma.Read(start + maxIndex) < ma.Read(start + rightIndex))
                    {
                        maxIndex = rightIndex;
                    }
                }
                if (maxIndex == root)
                {
                    break;
                }

                ma.Swap(start + maxIndex, start + root, token);

                root = maxIndex;
            }
        }
コード例 #2
0
 static public void Copy(IMemoryAcessor source, long sourceIndex, IMemoryAcessor destination, long destinationIndex, long length, CancellationToken token)
 {
     for (long index = 0; index < length; ++index)
     {
         token.ThrowIfCancellationRequested();
         destination.Write(destinationIndex + index, source.Read(sourceIndex + index));
     }
 }
コード例 #3
0
        static void Merge(IMemoryAcessor aux, IMemoryAcessor ma, long start, long mid, long end, CancellationToken token)
        {
            Utils.Copy(ma, start, aux, start, mid - start, token);
            long left  = start;
            long right = mid;
            long index = start;

            while (left < mid && right < end)
            {
                token.ThrowIfCancellationRequested();
                if (aux.Read(left) < ma.Read(right))
                {
                    ma.Write(index++, aux.Read(left++));
                }
                else
                {
                    ma.Write(index++, ma.Read(right++));
                }
            }
            Utils.Copy(aux, left, ma, index, mid - left, token);
        }
コード例 #4
0
        static long PartArray(IMemoryAcessor ma, long leftIndex, long rightIndex, CancellationToken token)
        {
            long pivotIndex = leftIndex + ((rightIndex - leftIndex + 1) >> 1);

            ma.Swap(leftIndex, pivotIndex, token);

            UInt16 pivot = ma.Read(leftIndex);
            long   left  = leftIndex;
            long   right = rightIndex + 1;

            while (true)
            {
                while (ma.Read(++left) < pivot)
                {
                    if (left == rightIndex)
                    {
                        break;
                    }
                }
                while (ma.Read(--right) > pivot)
                {
                    if (right == leftIndex)
                    {
                        break;
                    }
                }

                if (left >= right)
                {
                    break;
                }
                ma.Swap(left, right, token);
            }

            ma.Swap(leftIndex, right, token);
            return(right);
        }