Theory of Computing Systems

, Volume 59, Issue 2, pp 209–230

# QuickHeapsort: Modifications and Improved Analysis

• Volker Diekert
• Armin Weiß
Article

## Abstract

QuickHeapsort is a combination of Quicksort and Heapsort. We show that the expected number of comparisons for QuickHeapsort is always better than for Quicksort if a usual median-of-constant strategy is used for choosing pivot elements. In order to obtain the result we present a new analysis for QuickHeapsort splitting it into the analysis of the partition-phases and the analysis of the heap-phases. This enables us to consider samples of non-constant size for the pivot selection and leads to better theoretical bounds for the algorithm. Furthermore, we introduce some modifications of QuickHeapsort. We show that for every input the expected number of comparisons is at most $$n\log _{2}n - 0.03n + o(n)$$ for the in-place variant. If we allow n extra bits, then we can lower the bound to $$n\log _{2} n -0.997 n+ o (n)$$. Thus, spending n extra bits we can save more that 0.96n comparisons if n is large enough. Both estimates improve the previously known results. Moreover, our non-in-place variant does essentially use the same number of comparisons as index based Heapsort variants and Relaxed-Weak-Heapsort which use $$n\log _{2}n -0.9 n+ o (n)$$ comparisons in the worst case. However, index based Heapsort variants and Relaxed-Weak-Heapsort require $${\Theta }(n\log n)$$ extra bits whereas we need n bits only. Our theoretical results are upper bounds and valid for every input. Our computer experiments show that the gap between our bounds and the actual values on random inputs is small. Moreover, the computer experiments establish QuickHeapsort as competitive with Quicksort in terms of running time.

## Keywords

In-place sorting Heapsort Quicksort Analysis of algorithms

## Notes

### Acknowledgments

We thank Martin Dietzfelbinger, Stefan Edelkamp, Jyrki Katajainen and the anonymous referees for various comments which improved the presentation of the paper. We also thank Simon Paridon for helping us with the implementation of the algorithms.

### Conflict of interests

The authors declare that they have no conflict of interest.

## References

1. 1.
Blum, M., Floyd, R.W., Pratt, V., Rivest, R.L., Tarjan, R.E.: Time bounds for selection. J. Comput. Syst. Sci. 7(4), 448–461 (1973)
2. 2.
Cantone, D., Cincotti, G.: QuickHeapsort, an efficient mix of classical sorting algorithms. Theor. Comput. Sci. 285(1), 25–42 (2002)
3. 3.
Carlsson, S., Chen, J., Mattsson, C.: Heaps with Bits. In: D.-Z. Du, X.-S. Zhang (eds.) ISAAC, vol. 834 of LNCS, pp. 288–296. Springer (1994)Google Scholar
4. 4.
Chen, J.: A Framework for Constructing Heap-like structures in-place. In: K.-W. Ng, et al. (eds.) ISAAC, vol. 762 of LNCS, pp. 118–127. Springer (1993)Google Scholar
5. 5.
Chen, J., Edelkamp, S., Elmasry, A., Katajainen, J.: In-place Heap Construction with Optimized Comparisons, Moves, and Cache Misses. In: B. Rovan, V. Sassone, P. Widmayer (eds.) MFCS, vol. 7464 of LNCS, pp. 259–270. Springer (2012)Google Scholar
6. 6.
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms. The MIT Press, 3rd edn. (2009)Google Scholar
7. 7.
Dutton, R.D.: Weak-heap sort. BIT 33(3), 372–381 (1993)
8. 8.
Edelkamp, S., Stiegeler, P.: Implementing HEAPSORT with $$n \lg n - 0.9n$$ and QUICKSORT with $$n \lg n + 0.2n$$ comparisons. ACM J. of Exp. Alg., 7:5 (2002)Google Scholar
9. 9.
Floyd, R.W.: Algorithm 245: Treesort. Commun. ACM 7(12), 701 (1964)
10. 10.
Gonnet, G.H., Munro, J.I.: Heaps on Heaps. SIAM J. Comput. 15(4), 964–971 (1986)
11. 11.
Katajainen, J.: The ultimate heapsort. In: X. Lin (ed.) CATS, vol. 20 of Australian Computer Science Communications, pp. 87–96. Springer-Verlag (1998)Google Scholar
12. 12.
Knuth, D.E.: The art of computer programming. vol. 3 Addison-Wesley (1998)Google Scholar
13. 13.
Martínez, C., Roura, S.: Optimal sampling strategies in Quicksort and Quickselect. SIAM J. Comput. 31(3), 683–705 (2001)
14. 14.
McDiarmid, C., Reed, B.A.: Building Heaps Fast. J. Alg. 10(3), 352–365 (1989)
15. 15.
Reinhardt, K.: Sorting in-place with a worst case complexity of $$n \log n -1.3 n + O(\log n)$$ comparisons and $$\epsilon n \log n + O(1)$$ transports. In: T. Ibaraki, et al. (eds.) ISAAC, vol. 650 of LNCS, pp. 489–498. Springer (1992)Google Scholar
16. 16.
Wang, X.-D., Wu, Y.-J.: An improved HEAPSORT Algorithm with $$n\lg n - 0.788928n$$ comparisons in the Worst Case. J. Comput. Sci. Techn. 22, 898–903 (2007)
17. 17.
Wegener, I.: The worst case complexity of McDiarmid and Reed’s variant of Bottom-Up-Heap sort is less than $$n \lg n + 1.1n$$. In: C. Choffrut, M. Jantzen (eds.) STACS, vol. 480 of LNCS, pp. 137–147. Springer (1991)Google Scholar
18. 18.
Wegener, I.: BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT, beating, on an average, QUICKSORT (if n is not very small). Theor. Comp. Sci. 118(1), 81–98 (1993)