Heap construction: Optimal in both worst and average cases?
We investigate the complexity of constructing heaps. The heap construction problem has been extensively studied. However, there was no algorithm for building heaps that is optimal in both the worst and average cases simultaneously. In particular, the worst-case fastest algorithm, proposed by Gonnet and Munro, takes 1.625n comparisons to build an n-element heap (with an average cost of 1.5803n comparisons). The best known average-case upper bound of 1.5212n comparisons was derived by McDiarmid and Reed, which has a worst-case performance of 2n comparisons. Both algorithms require extra space and were conjectured to be optimal respectively in the worst and the average case. In this paper, we design a heap construction algorithm that takes at most 1.625n and 1.500n comparisons in the worst and average cases, respectively. Our algorithm not only improves over the previous best known average-case result by McDiarmid and Reed, but also achieves the best known worst-case upper bound due to Gonnet and Munro. Moreover, we also show that a heap on n elements can be constructed in-situ using at most 1.528n comparisons on average and 2n comparisons in the worst case. This is only 0.007n comparisons more than that of McDiarmid and Reed's algorithm, while the latter needs n bits of extra space.
Unable to display preview. Download preview PDF.
- 1.A.V. Aho, J.E. Hopcroft, and J.D. Ullman. The Design and Analysis of Computer Algorithms. Addison-Wesley, Reading, Massachusetts, 1974.Google Scholar
- 2.B. Bollobás and I. Simon. Repeated random insertion into a priority queue. Journal of Algorithms, 6:466–477, 1985.Google Scholar
- 3.S. Carlsson. Average-case results on heapsort. BIT, 27:2–17, 1987.Google Scholar
- 4.S. Carlsson. A variant of heapsort with almost optimal number of comparisons. Information Processing Letters, 24:247–250, 1987.Google Scholar
- 5.S. Carlsson and J. Chen. The complexity of heaps. In Proceedings of the Third Annual ACM-SIAM Symposium on Discrete Algorithms, pages 393–402. Orlando, Florida, USA, January 1992.Google Scholar
- 6.W. Cunto and J.I. Munro. Average case selection. Journal of the ACM, 36:270–279, 1989.Google Scholar
- 7.E.-E. Doberkat. An average case analysis of Floyd's algorithm to construct heaps. Information and Control, 61:114–131, 1984.Google Scholar
- 8.R.W. Floyd. Algorithm 245 — Treesort 3. Communications of the ACM, 7:701, 1964.Google Scholar
- 9.A.M. Frieze. On the random construction of heaps. Information Processing Letters, 27(2):103–109, 1988.Google Scholar
- 10.G.H. Gonnet and J.I. Munro. Heaps on heaps. SIAM Journal on Computing, 15:964–971, 1986.Google Scholar
- 11.R. Hayward and C.J.H. McDiarmid. Average case analysis of heap building by repeated insertion. Journal of Algorithms, 12(1):126–153, 1991.Google Scholar
- 12.D.E. Knuth. The Art of Computer Programming, Vol. 3: Sorting and Searching. Addison-Wesley, Reading, Massachusetts, 1973.Google Scholar
- 13.C.J.H. McDiarmid and B.A. Reed. Building heaps fast. Journal of Algorithms, 10:352–365, 1989.Google Scholar
- 14.J. Vuillemin. A data structure for manipulating priority queues. Communications of the ACM, 21:309–314, 1978.Google Scholar
- 15.I. Wegener. Bottom-up-heapsort, a new variant of HEAPSORT beating, on an average, QUICKSORT (if n is not very small). Theoretical Computer Science, 118:81–98, 1993.Google Scholar
- 16.J.W.J. Williams. Algorithm 232: Heapsort. Communications of the ACM, 7:347–348, 1964.Google Scholar