예제 #1
0
        public static int[] HeapSort(int[] inputArray)
        {
            var tempArray = new int[inputArray.Length];

            tempArray = inputArray.Select(item => item).ToArray();

            var heap = new MaxHeap <int>();

            foreach (var item in tempArray)
            {
                heap.Add(item);
            }

            for (var i = tempArray.Length - 1; i >= 0; i--)
            {
                tempArray[i] = heap.RemoveMaxAndReturnItsValue();
            }

            return(tempArray);
        }
예제 #2
0
        private static void MaxHeapTest()
        {
            var intHeap = new MaxHeap <int>();

            intHeap.Add(24);
            intHeap.Add(37);
            intHeap.Add(17);
            intHeap.Add(28);
            intHeap.Add(31);
            intHeap.Add(29);
            intHeap.Add(15);
            intHeap.Add(12);
            intHeap.Add(20);
            intHeap.Add(40);

            WriteHeapToConsole(intHeap);

            while (intHeap.Count > 0)
            {
                intHeap.RemoveMaxAndReturnItsValue();
                WriteHeapToConsole(intHeap);
            }
        }