예제 #1
0
        private static void SiftDown(long[] array, long i)
        {
            while (true)
            {
                var minIndex = i;
                var l        = GetLeftChildIndex(i);
                if (l < array.Length && array[l] < array[minIndex])
                {
                    minIndex = l;
                }

                var r = GetRightChildIndex(i);
                if (r < array.Length && array[r] < array[minIndex])
                {
                    minIndex = r;
                }

                if (minIndex != i)
                {
                    SwapCounter++;
                    StringsToPrint2.AppendLine($"{minIndex} {i}");
                    var temp = array[minIndex];
                    array[minIndex] = array[i];
                    array[i]        = temp;
                    i = minIndex;
                    continue;
                }

                break;
            }
        }
예제 #2
0
        private static void SiftUp(long[] array, long i)
        {
            while (true)
            {
                var parentIndex = GetParent(i);
                if (parentIndex < 0)
                {
                    return;
                }

                if (array[parentIndex] > array[i])
                {
                    SwapCounter++;
                    StringsToPrint2.AppendLine($"{parentIndex} {i}");
                    var temp = array[parentIndex];
                    array[parentIndex] = array[i];
                    array[i]           = temp;
                    i = parentIndex;
                    continue;
                }

                break;
            }
        }