In-place Heap Construction with Optimized Comparisons, Moves, and Cache Misses

  • Jingsen Chen
  • Stefan Edelkamp
  • Amr Elmasry
  • Jyrki Katajainen
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7464)


We show how to build a binary heap in-place in linear time by performing ~ 1.625n element comparisons, at most ~ 2.125n element moves, and ~ n/B cache misses, where n is the size of the input array, B the capacity of the cache line, and ~ f(n) approaches f(n) as n grows. The same bound for element comparisons was derived and conjectured to be optimal by Gonnet and Munro; however, their procedure requires Θ(n) pointers and does not have optimal cache behaviour. Our main idea is to mimic the Gonnet-Munro algorithm by converting a navigation pile into a binary heap. To construct a binary heap in-place, we use this algorithm to build bottom heaps of size \(\Theta(\lg n)\) and adjust the heap order at the upper levels using Floyd’s sift-down procedure. On another frontier, we compare different heap-construction alternatives in practice.


Minimum Element Cache Line Element Move Output Area Element Comparison 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.


  1. 1.
    Bojesen, J., Katajainen, J., Spork, M.: Performance engineering case study: Heap construction. ACM J. Exp. Algorithmics 5, Article 15 (2000)Google Scholar
  2. 2.
    Carlsson, S.: A variant of heapsort with almost optimal number of comparisons. Inform. Process. Lett. 24(4), 247–250 (1987)MathSciNetzbMATHCrossRefGoogle Scholar
  3. 3.
    Chen, J.: A Framework for Constructing Heap-Like Structures in-Place. In: Ng, K.W., Balasubramanian, N.V., Raghavan, P., Chin, F.Y.L. (eds.) ISAAC 1993. LNCS, vol. 762, pp. 118–127. Springer, Heidelberg (1993)CrossRefGoogle Scholar
  4. 4.
    Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. The MIT Press, Cambridge (2009)zbMATHGoogle Scholar
  5. 5.
    Dutton, R.D.: Weak-heap sort. BIT 33(3), 372–381 (1993)MathSciNetCrossRefGoogle Scholar
  6. 6.
    Floyd, R.W.: Algorithm 245: Treesort 3. Commun. ACM 7(12), 701 (1964)CrossRefGoogle Scholar
  7. 7.
    Frigo, M., Leiserson, C.E., Prokop, H., Ramachandra, S.: Cache-oblivious algorithms. In: 40th Annual Symposium on Foundations of Computer Science, pp. 285–297. IEEE Computer Society, Los Alamitos (1999)Google Scholar
  8. 8.
    Gonnet, G.H., Munro, J.I.: Heaps on heaps. SIAM J. Comput. 15(4), 964–971 (1986)MathSciNetzbMATHCrossRefGoogle Scholar
  9. 9.
    Katajainen, J., Vitale, F.: Navigation piles with applications to sorting, priority queues, and priority deques. Nordic J. Comput. 10(3), 238–262 (2003)MathSciNetzbMATHGoogle Scholar
  10. 10.
    Kronrod, M.A.: Optimal ordering algorithm without operational field. Soviet Math. Dokl. 10, 744–746 (1969)zbMATHGoogle Scholar
  11. 11.
    Li, Z., Reed, B.A.: Heap Building Bounds. In: Dehne, F., López-Ortiz, A., Sack, J.-R. (eds.) WADS 2005. LNCS, vol. 3608, pp. 14–23. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  12. 12.
    McDiarmid, C.J.H., Reed, B.A.: Building heaps fast. J. Algorithms 10(3), 352–365 (1989)MathSciNetzbMATHCrossRefGoogle Scholar
  13. 13.
    Vuillemin, J.: A data structure for manipulating priority queues. Commun. ACM 21(4), 309–315 (1978)MathSciNetzbMATHCrossRefGoogle Scholar
  14. 14.
    Wegener, I.: Bottom-up-heapsort, a new variant of heapsort beating, on an average, quicksort (if n is not very small). Theoret. Comput. Sci. 118(1), 81–98 (1993)MathSciNetzbMATHCrossRefGoogle Scholar
  15. 15.
    Wegener, I.: The worst case complexity of McDiarmid and Reed’s variant of bottom-up heapsort is less than nlogn + 1. 1n. Inform. and Comput. 97(1), 86–96 (1992)MathSciNetzbMATHCrossRefGoogle Scholar
  16. 16.
    Williams, J.W.J.: Algorithm 232: Heapsort. Commun. ACM 7(6), 347–348 (1964)Google Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2012

Authors and Affiliations

  • Jingsen Chen
    • 1
  • Stefan Edelkamp
    • 2
  • Amr Elmasry
    • 3
  • Jyrki Katajainen
    • 3
  1. 1.Department of Computer Science, Electrical and Space EngineeringLuleå University of TechnologyLuleåSweden
  2. 2.Faculty 3—Mathematics and Computer ScienceUniversity of BremenBremenGermany
  3. 3.Department of Computer ScienceUniversity of CopenhagenCopenhagen EastDenmark

Personalised recommendations