Ejemplo n.º 1
0
        // The algorithm builds a heap. It then repeatedly swaps the first
        // and last items in the heap, and rebuilds the heap excluding the last item.
        // O(N log N)
        public static void HeapSort <T>(this T[] arr)
            where T : IComparable <T>
        {
            Heap <T> heap = new Heap <T>(arr.Length);

            // N log N
            foreach (T item in arr)
            {
                heap.Push(item);
            }
            // + N log N
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = heap.Pop();
            }
        }