On the worst-case complexity of timsort. (English) Zbl 07378674

Azar, Yossi (ed.) et al., 26th annual European symposium on algorithms, ESA 2018, August 20–22, 2018, Helsinki, Finland. Proceedings. Wadern: Schloss Dagstuhl – Leibniz Zentrum für Informatik. LIPIcs – Leibniz Int. Proc. Inform. 112, Article 4, 13 p. (2018).
Summary: TimSort is an intriguing sorting algorithm designed in 2002 for Python, whose worst-case complexity was announced, but not proved until our recent preprint. In fact, there are two slightly different versions of TimSort that are currently implemented in Python and in Java respectively. We propose a pedagogical and insightful proof that the Python version runs in \(\mathcal{O}(n\log n)\). The approach we use in the analysis also applies to the Java version, although not without very involved technical details. As a byproduct of our study, we uncover a bug in the Java implementation that can cause the sorting method to fail during the execution. We also give a proof that Python’s TimSort running time is in \(\mathcal{O}(n+n\log\rho)\), where \(\rho\) is the number of runs (i.e. maximal monotonic sequences), which is quite a natural parameter here and part of the explanation for the good behavior of TimSort on partially sorted inputs.
For the entire collection see [Zbl 1393.68010].


68Wxx Algorithms in computer science


Full Text: DOI arXiv


[2] Nicolas Auger, Cyril Nicaud, and Carine Pivoteau. Merge strategies: From Merge Sort to TimSort. Research Report hal-01212839, hal, 2015. URL: https://hal-upec-upem. archives-ouvertes.fr/hal-01212839.
[3] Jérémy Barbay and Gonzalo Navarro. On compressing permutations and adaptive sorting. Theor. Comput. Sci., 513:109-123, 2013. doi:10.1016/j.tcs.2013.10.019. · Zbl 1358.68079
[4] Bernhard Beckert, Reiner Hähnle, and Peter H Schmitt. Verification of object-oriented software: The KeY approach. Springer-Verlag, 2007. · Zbl 1202.68092
[5] Sam Buss and Alexander Knop. Strategies for stable merge sorting. Research Report abs/1801.04641, arXiv, 2018. URL: http://arxiv.org/abs/1801.04641. · Zbl 1431.68021
[6] Stijn De Gouw, Jurriaan Rot, Frank S de Boer, Richard Bubel, and Reiner Hähnle. Open-JDK’s Java.utils.Collection.sort() is broken: The good, the bad and the worst case. In International Conference on Computer Aided Verification, pages 273-289. Springer, 2015. · Zbl 1468.68125
[7] Donald E. Knuth. The Art of Computer Programming, Volume 3: (2nd Ed.) Sorting and Searching. Addison Wesley Longman Publish. Co., Redwood City, CA, USA, 1998. · Zbl 0895.68054
[8] Heikki Mannila. Measures of presortedness and optimal sorting algorithms. IEEE Trans. Computers, 34(4):318-325, 1985. doi:10.1109/TC.1985.5009382. · Zbl 0556.68031
[9] J. Ian Munro and Sebastian Wild. Nearly-optimal mergesorts: Fast, practical sorting methods that optimally adapt to existing runs. In Hannah Bast Yossi Azar and Grzegorz Herman, editors, 26th Annual European Symposium on Algorithms (ESA 2018), Leibniz International Proceedings in Informatics (LIPIcs), pages 63:1-63:15, 2018.
[10] Tim Peters. Timsort description, accessed june 2015. URL: http://svn.python.org/ projects/python/trunk/Objects/listsort.txt.
[11] Tadao Takaoka. Partial solution and entropy. In Rastislav Královič and Damian Niwiński, editors, Mathematical Foundations of Computer Science 2009, pages 700-711, Berlin, Hei-delberg, 2009. Springer Berlin Heidelberg · Zbl 1250.68132
This reference list is based on information provided by the publisher or from digital mathematics libraries. Its items are heuristically matched to zbMATH identifiers and may contain data conversion errors. It attempts to reflect the references listed in the original paper as accurately as possible without claiming the completeness or perfect precision of the matching.