Esempio n. 1
0
        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;
            }
        }
Esempio n. 2
0
        static void HeapSort(IMemoryAcessor ma, long start, long end, CancellationToken token)
        {
            Heapify(ma, start, end, token);

            for (long size = end - start; size > 0; --size)
            {
                SinkDown(ma, 0, start, start + size, token);
                ma.Swap(start, start + size - 1, token);
            }
        }
Esempio n. 3
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);
        }