Document Open Access Logo

Nearly-Optimal Mergesorts: Fast, Practical Sorting Methods That Optimally Adapt to Existing Runs

Authors J. Ian Munro , Sebastian Wild

Thumbnail PDF


  • Filesize: 0.62 MB
  • 16 pages

Document Identifiers

Author Details

J. Ian Munro
  • University of Waterloo, Canada
Sebastian Wild
  • University of Waterloo, Canada

Cite AsGet BibTex

J. Ian Munro and Sebastian Wild. Nearly-Optimal Mergesorts: Fast, Practical Sorting Methods That Optimally Adapt to Existing Runs. In 26th Annual European Symposium on Algorithms (ESA 2018). Leibniz International Proceedings in Informatics (LIPIcs), Volume 112, pp. 63:1-63:16, Schloss Dagstuhl - Leibniz-Zentrum für Informatik (2018)


We present two stable mergesort variants, "peeksort" and "powersort", that exploit existing runs and find nearly-optimal merging orders with negligible overhead. Previous methods either require substantial effort for determining the merging order (Takaoka 2009; Barbay & Navarro 2013) or do not have an optimal worst-case guarantee (Peters 2002; Auger, Nicaud & Pivoteau 2015; Buss & Knop 2018) . We demonstrate that our methods are competitive in terms of running time with state-of-the-art implementations of stable sorting methods.

Subject Classification

ACM Subject Classification
  • Theory of computation → Sorting and searching
  • adaptive sorting
  • nearly-optimal binary search trees
  • Timsort


  • Access Statistics
  • Total Accesses (updated on a weekly basis)
    PDF Downloads


  1. Nicolas Auger, Vincent Jugé, Cyril Nicaud, and Carine Pivoteau. On the worst-case complexity of TimSort. In Hannah Bast Yossi Azar and Grzegorz Herman, editors, 26th Annual European Symposium on Algorithms (ESA 2018), Leibniz International Proceedings in Informatics (LIPIcs), 2018. URL:
  2. Nicolas Auger, Cyril Nicaud, and Carine Pivoteau. Merge strategies: from merge sort to TimSort, 2015. URL:
  3. Jérémy Barbay, Johannes Fischer, and Gonzalo Navarro. LRM-trees: Compressed indices, adaptive sorting, and compressed permutations. Theoretical Computer Science, 459:26-41, 2012. URL:
  4. Jérémy Barbay and Gonzalo Navarro. Compressed representations of permutations, and applications. In Susanne Albers and Jean-Yves Marion, editors, 26th International Symposium on Theoretical Aspects of Computer Science (STACS 2009), pages 111-122, Freiburg, Germany, 2009. URL:
  5. Jérémy Barbay and Gonzalo Navarro. On compressing permutations and adaptive sorting. Theoretical Computer Science, 513:109-123, 2013. URL:
  6. Jérémy Barbay, Carlos Ochoa, and Srinivasa Rao Satti. Synergistic solutions on multisets. In Juha Kärkkäinen, Jakub Radoszewski, and Wojciech Rytter, editors, 28th Annual Symposium on Combinatorial Pattern Matching (CPM 2017), Leibniz International Proceedings in Informatics (LIPIcs), pages 31:1-31:14, 2017. URL:
  7. Paul J. Bayer. Improved Bounds on the Cost of Optimal and Balanced Binary Search Trees. Master’s thesis, Massachusetts Institute of Technology, 1975. Google Scholar
  8. Jon L. Bentley and M. Douglas McIlroy. Engineering a sort function. Software: Practice and Experience, 23(11):1249-1265, 1993. URL:
  9. Sam Buss and Alexander Knop. Strategies for stable merge sorting, 2018. URL:
  10. Stijn de Gouw, Frank S. de Boer, Richard Bubel, Reiner Hähnle, Jurriaan Rot, and Dominic Steinhöfel. Verifying OpenJDK’s sort method for generic collections. Journal of Automated Reasoning, aug 2017. URL:
  11. Amr Elmasry and Abdelrahman Hammad. Inversion-sensitive sorting algorithms in practice. Journal of Experimental Algorithmics, 13:11:1-11:18, 2009. URL:
  12. Vladmir Estivill-Castro and Derick Wood. A survey of adaptive sorting algorithms. ACM Computing Surveys, 24(4):441-476, dec 1992. URL:
  13. Harold N. Gabow, Jon Louis Bentley, and Robert E. Tarjan. Scaling and related techniques for geometry problems. In 16th annual ACM symposium on Theory of computing (STOC 1984), pages 135-143. ACM Press, 1984. URL:
  14. Mordecai J. Golin and Robert Sedgewick. Queue-mergesort. Information Processing Letters, 48(5):253-259, dec 1993. URL:
  15. Chris Hegarty. Replace "modified mergesort" in java.util.Arrays.sort with timsort, 2009. URL:
  16. Yasuichi Horibe. An improved bound for weight-balanced tree. Information and Control, 34(2):148-151, jun 1977. URL:
  17. Donald E. Knuth. The Art Of Computer Programming: Searching and Sorting. Addison Wesley, 2nd edition, 1998. Google Scholar
  18. Kurt Mehlhorn. Nearly optimal binary search trees. Acta Informatica, 5(4), 1975. URL:
  19. Kurt Mehlhorn. A best possible bound for the weighted path length of binary search trees. SIAM Journal on Computing, 6(2):235-239, 1977. URL:
  20. Kurt Mehlhorn. Sorting presorted files. In Theoretical Computer Science 4th GI Conference, pages 199-212. Springer, 1979. URL:
  21. Kurt Mehlhorn. Data Structures and Algorithms 1: Sorting and Searching. Springer, 1984. Google Scholar
  22. Ian Munro and Philip M. Spira. Sorting and searching in multisets. SIAM Journal on Computing, 5(1):1-8, 1976. URL:
  23. S.V. Nagaraj. Optimal binary search trees. Theoretical Computer Science, 188(1-2):1-44, nov 1997. URL:
  24. Tim Peters. [Python-Dev] Sorting, 2002. URL:
  25. Tim Peters. Timsort, 2002. URL:
  26. Robert Sedgewick. Quicksort with equal keys. SIAM Journal on Computing, 6(2):240-267, 1977. Google Scholar
  27. Robert Sedgewick. Algorithms in Java. Addison-Wesley, 2003. Google Scholar
  28. Robert Sedgewick and Jon Bentley. Quicksort is optimal (talk slides), 2002. URL:
  29. Tadao Takaoka. A new measure of disorder in sorting - entropy. In The Fourth Australasian Theory Symposium (CATS 1998), pages 77-86, 1998. Google Scholar
  30. Tadao Takaoka. Partial solution and entropy. In MFCS 2009, pages 700-711, 2009. URL:
  31. W.A. Walker and C.C. Gotlieb. A top-down algorithm for constructing nearly optimal lexicographic trees. In Graph Theory and Computing, pages 303-323. Elsevier, 1972. URL:
  32. Lutz M. Wegner. Quicksort for equal keys. IEEE Transactions on Computers, C-34(4):362-367, 1985. URL:
  33. Sebastian Wild. Accompanying code for running time study, 2018. URL:
  34. Sebastian Wild. Quicksort is optimal for many equal keys. In ANALCO 2018, pages 8-22. SIAM, jan 2018. URL:
Questions / Remarks / Feedback

Feedback for Dagstuhl Publishing

Thanks for your feedback!

Feedback submitted

Could not send message

Please try again later or send an E-mail