Branch Mispredictions Don’t Affect Mergesort

  • Amr Elmasry
  • Jyrki Katajainen
  • Max Stenmark
Part of the Lecture Notes in Computer Science book series (LNCS, volume 7276)


In quicksort, due to branch mispredictions, a skewed pivot-selection strategy can lead to a better performance than the exact-median pivot-selection strategy, even if the exact median is given for free. In this paper we investigate the effect of branch mispredictions on the behaviour of mergesort. By decoupling element comparisons from branches, we can avoid most negative effects caused by branch mispredictions. When sorting a sequence of n elements, our fastest version of mergesort performs n log2 n + O(n) element comparisons and induces at most O(n) branch mispredictions. We also describe an in-situ version of mergesort that provides the same bounds, but uses only O(log2 n) words of extra memory. In our test computers, when sorting integer data, mergesort was the fastest sorting method, then came quicksort, and in-situ mergesort was the slowest of the three. We did a similar kind of decoupling for quicksort, but the transformation made it slower.


Code Block Cache Line Program Transformation Element Move Extra Space 
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.
    Biggar, P., Nash, N., Williams, K., Gregg, D.: An Experimental Study of Sorting and Branch Prediction. ACM J. Exp. Algorithmics 12, Article 1.8 (2008)Google Scholar
  2. 2.
    Brodal, G.S., Moruz, G.: Tradeoffs Between Branch Mispredictions and Comparisons for Sorting Algorithms. In: Dehne, F., López-Ortiz, A., Sack, J.-R. (eds.) WADS 2005. LNCS, vol. 3608, pp. 385–395. Springer, Heidelberg (2005)CrossRefGoogle Scholar
  3. 3.
    Brodal, G.S., Moruz, G.: Skewed Binary Search Trees. In: Azar, Y., Erlebach, T. (eds.) ESA 2006. LNCS, vol. 4168, pp. 708–719. Springer, Heidelberg (2006)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)Google Scholar
  5. 5.
    Elmasry, A., Katajainen, J.: Lean Programs, Branch Mispredictions, and Sorting. In: Kranakis, E., Krizanc, D., Luccio, F. (eds.) FUN 2012. LNCS, vol. 7288, pp. 119–130. Springer, Heidelberg (2012)Google Scholar
  6. 6.
    Hoare, C.A.R.: Quicksort. Comput. J.  5(1), 10–16 (1962)MathSciNetzbMATHCrossRefGoogle Scholar
  7. 7.
    Intel Corporation: Intel® 64 and IA-32 Architectures Optimization Reference Manual, version 025, Santa Clara (1997–2011)Google Scholar
  8. 8.
    Kaligosi, K., Sanders, P.: How Branch Mispredictions Affect Quicksort. In: Azar, Y., Erlebach, T. (eds.) ESA 2006. LNCS, vol. 4168, pp. 780–791. Springer, Heidelberg (2006)CrossRefGoogle Scholar
  9. 9.
    Katajainen, J., Pasanen, T., Teuhola, J.: Practical in-place mergesort. Nordic J. Comput. 3(1), 27–40 (1996)MathSciNetGoogle Scholar
  10. 10.
    Katajainen, J., Träff, J.L.: A Meticulous Analysis of Mergesort Programs. In: Bongiovanni, G., Bovet, D.P., Di Battista, G. (eds.) CIAC 1997. LNCS, vol. 1203, pp. 217–228. Springer, Heidelberg (1997)CrossRefGoogle Scholar
  11. 11.
    Kernighan, B.W., Ritchie, D.M.: The C Programming Language, 2nd edn. Prentice Hall, Englewood Cliffs (1988)Google Scholar
  12. 12.
    Mortensen, S.: Refining the Pure-C Cost Model. Master’s Thesis, Department of Computer Science. University of Copenhagen, Copenhagen (2001)Google Scholar
  13. 13.
    Musser, D.R.: Introspective Sorting and Selection Algorithms. Software Pract. Exper.  27(8), 983–993 (1997)CrossRefGoogle Scholar
  14. 14.
    Patterson, D.A., Hennessy, J.L.: Computer Organization and Design, The Hardware/Software Interface, 4th edn. Morgan Kaufmann Publishers, Burlington (2009)Google Scholar
  15. 15.
    Sanders, P., Winkel, S.: Super Scalar Sample Sort. In: Albers, S., Radzik, T. (eds.) ESA 2004. LNCS, vol. 3221, pp. 784–796. Springer, Heidelberg (2004)CrossRefGoogle Scholar
  16. 16.
    Sedgewick, R.: The Analysis of Quicksort Programs. Acta Inform.  7(4), 327–355 (1977)MathSciNetzbMATHCrossRefGoogle Scholar

Copyright information

© Springer-Verlag Berlin Heidelberg 2012

Authors and Affiliations

  • Amr Elmasry
    • 1
  • Jyrki Katajainen
    • 1
    • 2
  • Max Stenmark
    • 2
  1. 1.Department of Computer ScienceUniversity of CopenhagenCopenhagen EastDenmark
  2. 2.Jyrki Katajainen and CompanyCopenhagen NorthDenmark

Personalised recommendations