Exemplo n.º 1
0
        static void Main(string[] args)
        {
            HeapSort heapsorter = new HeapSort();

            //Unsorted array
            int[] array = { 1, 14, 10, 9, 3, 6, 2, 8, 5, 7, 16, 4 };

            //Create a max heap
            for (int i = array.Length / 2 - 1; i >= 0; i--)
            {
                heapsorter.heapify(array, i, array.Length);
            }

            //Sift down the heap
            for (int i = array.Length - 1; i >= 0; i--)
            {
                heapsorter.swap(array, 0, i);
                heapsorter.heapify(array, 0, i);
            }

            //Output result
            Console.Write("Sorted array: ");
            foreach (var item in array)
            {
                Console.Write(item.ToString() + " ");
            }
            Console.Write("\n");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // init
            var rnd   = new Random(1337);
            var items = new int[10];

            for (int i = 0; i <= 9; i++)
            {
                items[i] = rnd.Next(100);
            }

            Write("Input:", items);

            var heapSort = new HeapSort();
            var result   = heapSort.Sort(items);

            Write("Output:", result);
        }