Skip to main content

Branch Mispredictions Don’t Affect Mergesort

  • Conference paper
Book cover Experimental Algorithms (SEA 2012)

Part of the book series: Lecture Notes in Computer Science ((LNTCS,volume 7276))

Included in the following conference series:

Abstract

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 54.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Preview

Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

References

  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. 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)

    Chapter  Google Scholar 

  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)

    Chapter  Google Scholar 

  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. 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. Hoare, C.A.R.: Quicksort. Comput. J.  5(1), 10–16 (1962)

    Article  MathSciNet  MATH  Google Scholar 

  7. Intel Corporation: Intel® 64 and IA-32 Architectures Optimization Reference Manual, version 025, Santa Clara (1997–2011)

    Google Scholar 

  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)

    Chapter  Google Scholar 

  9. Katajainen, J., Pasanen, T., Teuhola, J.: Practical in-place mergesort. Nordic J. Comput. 3(1), 27–40 (1996)

    MathSciNet  Google Scholar 

  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)

    Chapter  Google Scholar 

  11. Kernighan, B.W., Ritchie, D.M.: The C Programming Language, 2nd edn. Prentice Hall, Englewood Cliffs (1988)

    Google Scholar 

  12. Mortensen, S.: Refining the Pure-C Cost Model. Master’s Thesis, Department of Computer Science. University of Copenhagen, Copenhagen (2001)

    Google Scholar 

  13. Musser, D.R.: Introspective Sorting and Selection Algorithms. Software Pract. Exper.  27(8), 983–993 (1997)

    Article  Google Scholar 

  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. 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)

    Chapter  Google Scholar 

  16. Sedgewick, R.: The Analysis of Quicksort Programs. Acta Inform.  7(4), 327–355 (1977)

    Article  MathSciNet  MATH  Google Scholar 

Download references

Author information

Authors and Affiliations

Authors

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2012 Springer-Verlag Berlin Heidelberg

About this paper

Cite this paper

Elmasry, A., Katajainen, J., Stenmark, M. (2012). Branch Mispredictions Don’t Affect Mergesort. In: Klasing, R. (eds) Experimental Algorithms. SEA 2012. Lecture Notes in Computer Science, vol 7276. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-642-30850-5_15

Download citation

  • DOI: https://doi.org/10.1007/978-3-642-30850-5_15

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-642-30849-9

  • Online ISBN: 978-3-642-30850-5

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics