Heap Construction - 50 Years Later

We study the problem of constructing a binary heap in an array using only a small amount of additional space. Let N denote the size of the input, M the capacity of the cache, and B the width of the cache lines of the underlying computer, all measured as numbers of elements. We show that there exists an in-place heapconstruction algorithm that runs in Θ(N) worst-case time and performs at most 1.625N +o(N) element comparisons, 1.5N +o(N) element moves, N/B+O(N/M · lgN) cache misses, and 1.375N + o(N) branch mispredictions. The same bound for the number of element comparisons was derived and conjectured to be optimal by Gonnet and Munro; however, their algorithm requires Θ(N) pointers. For a tuning parameter S, the idea is to divide the input into a top tree of size Θ(N/S) such that each of its leaves root a bottom tree of size Θ(S). When S = Θ(lgN/ lg lgN), we can convert the bottom trees into heaps one by one by packing the extra space needed in a few words, and subsequently use Floyd’s sift-down procedure to adjust the heap order at the upper levels. In addition to our theoretical findings, we also compare different heap-construction alternatives in practice.

[1]  J. Boothroyd,et al.  Algorithm 246: Graycode , 1964, CACM.

[2]  Donald Ervin Knuth,et al.  The Art of Computer Programming , 1968 .

[3]  Donald E. Knuth,et al.  Sorting and Searching , 1973 .

[4]  Jean Vuillemin,et al.  A data structure for manipulating priority queues , 1978, CACM.

[5]  Gaston H. Gonnet,et al.  Heaps on Heaps , 1982, SIAM J. Comput..

[6]  Svante Carlsson A Variant of Heapsort with Almost Optimal Number of Comparisons , 1987, Inf. Process. Lett..

[7]  Bruce A. Reed,et al.  Building Heaps Fast , 1989, J. Algorithms.

[8]  Ronald L. Rivest,et al.  Introduction to Algorithms , 1990 .

[9]  Ingo Wegener The Worst Case Complexity of McDiarmid and Reed's Variant of BOTTOM-UP HEAPSORT is less than nlog n + 1.1n , 1992, Inf. Comput..

[10]  Ingo Wegener BOTTOM-UP-HEAPSORT, a New Variant of HEAPSORT, Beating, on an Average, QUICKSORT (if n is not Very Small) , 1993, Theor. Comput. Sci..

[11]  Jingsen Chen,et al.  A Framework for Constructing Heap-Like Structures In-Place , 1993, ISAAC.

[12]  Ronald D. Dutton,et al.  Weak-heap sort , 1993, BIT.

[13]  Tomi Pasanen Elementary average case analysis of Floyd''s algorithms to construct heaps , 1996 .

[14]  Jesper Larsson Träff,et al.  A Meticulous Analysis of Mergesort Programs , 1997, CIAC.

[15]  Matteo Frigo,et al.  Cache-oblivious algorithms , 1999, 40th Annual Symposium on Foundations of Computer Science (Cat. No.99CB37039).

[16]  Jyrki Katajainen,et al.  Performance engineering case study: heap construction , 1999, JEAL.

[17]  Fabio Vitale,et al.  Navigation Piles with Applications to Sorting, Priority Queues, and Priority Deques , 2003, Nord. J. Comput..

[18]  Sebastian Winkel,et al.  Super Scalar Sample Sort , 2004, ESA.

[19]  Bruce A. Reed,et al.  Heap Building Bounds , 2005, WADS.

[20]  Ronald L. Rivest,et al.  Introduction to Algorithms, third edition , 2009 .

[21]  Robert E. Tarjan,et al.  Rank-Pairing Heaps , 2009, ESA.

[22]  Amr Elmasry,et al.  Branch Mispredictions Don't Affect Mergesort , 2012, SEA.

[23]  Amr Elmasry,et al.  Lean Programs, Branch Mispredictions, and Sorting , 2012, FUN.

[24]  Amr Elmasry,et al.  In-place Heap Construction with Optimized Comparisons, Moves, and Cache Misses , 2012, MFCS.

[25]  Amr Elmasry,et al.  Optimizing Binary Heaps , 2017, Theory of Computing Systems.