encyclopegia.org

Timsort

Timsort is a hybrid, stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.[src] It was implemented by Tim Peters in 2002 for use in the Python programming language.[src] The algorithm finds subsequences of the data that are already ordered (runs) and uses them to sort the remainder more efficiently.[src] This is done by merging runs until certain criteria are fulfilled.[src] Timsort was Python's standard sorting algorithm from version 2.3 until it was replaced in 3.11 by Powersort, a derived algorithm with a more robust merge policy.[src] Timsort is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, in GNU Octave, on V8, and in Swift.[src] Rust used a custom version of Timsort until May 2024.[src] The galloping technique derives from Carlsson, Levcopoulos, and O.[src] Petersson's 1990 paper "Sublinear merging and natural merge sort" and Peter McIlroy's 1993 paper "Optimistic Sorting and Information Theoretic Complexity".[src]

Timsort is a hybrid, stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data.[src] It was implemented by Tim Peters in 2002 for use in the Python programming language.[src] The algorithm finds subsequences of the data that are already ordered (runs) and uses them to sort the remainder more efficiently.[src] This is done by merging runs until certain criteria are fulfilled.[src] Timsort was Python's standard sorting algorithm from version 2.3 until it was replaced in 3.11 by Powersort, a derived algorithm with a more robust merge policy.[src] Timsort is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, in GNU Octave, on V8, and in Swift.[src] Rust used a custom version of Timsort until May 2024.[src] The galloping technique derives from Carlsson, Levcopoulos, and O.[src] Petersson's 1990 paper "Sublinear merging and natural merge sort" and Peter McIlroy's 1993 paper "Optimistic Sorting and Information Theoretic Complexity".[src] An individual merge of runs R1 and R2 keeps the count of consecutive elements selected from a run.[src] When this number reaches the minimum galloping threshold (min_gallop), Timsort considers that it is likely that many consecutive elements may still be selected from that run and switches into galloping mode.[src] Let us assume that R1 is responsible for triggering it.[src] In this mode, the algorithm performs a two-stage search for the place in the run R1 where the next element x of the run R2 would be inserted.[src] In the first stage it performs an exponential search, also known as a galloping search, until finding a k such that R1[2 k−1 − 1] k − 1], i.e.[src] a region of uncertainty comprising 2 k−1 − 1 consecutive elements of R1.[src] The second stage performs a straight binary search of this region to find the exact location in R1 for x.[src] Galloping mode is an attempt to adapt the merge algorithm to the pattern of intervals between elements in runs.[src] Galloping is not always efficient.[src] In some cases galloping mode requires more comparisons than a simple linear search.[src] According to benchmarks done by the developer, galloping is beneficial only when the initial element of one run is not one of the first seven elements of the other run.[src] This implies an initial threshold of 7.[src] To avoid the drawbacks of galloping mode, two actions are taken: (1) When galloping is found to be less efficient than binary search, galloping mode is exited.[src] (2) The success or failure of galloping is used to adjust min_gallop.[src] If the selected element is from the same array that returned an element previously, min_gallop is reduced by one, thus encouraging the return to galloping mode.[src] Otherwise, the value is incremented by one, thus discouraging a return to galloping mode.[src] In the case of random data, the value of min_gallop becomes so large that galloping mode never recurs.[src] \Eta=-\sum_{i=1}^r \frac{n_i}{n}\log_2 \frac{n_i}{n}.[src] When all run sizes are equal, the run-length entropy \log_2 r , its maximum value for any given number r of runs, but it can be smaller when the runs have unevenly distributed sizes.[src] The formula for the running time is given as n+n\Eta rather than more simply n\Eta , to account for the possibility that the entropy can be less than one.[src] The above behavior regarding the run-length entropy \Eta is purely derived by Timsort's merge criteria, as that is the part responsible for detecting already-sorted parts.[src] The "galloping" routine exploits a new property that can be described as the dual-run entropy \Eta^{*} : \Eta^{*}=-\sum_{i=1}^s \frac{n_i}{n}\log_2 \frac{n_i}{n}.[src] where s the number of times "galloping" is interrupted by and s \leq r .[src] It can be proven that TimSort takes up to O(n+n\Eta) element moves and O(n+n\Eta^{*}) value comparisons.[src]

42 sentences · 42 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Timsort." encyclopegia.org, 2026, https://encyclopegia.org/wiki/timsort. Accessed 29 November 2026.

Chicago

"Timsort." encyclopegia.org. Accessed November 29, 2026. https://encyclopegia.org/wiki/timsort.

APA

encyclopegia.org. (2026). Timsort. Retrieved November 29, 2026, from https://encyclopegia.org/wiki/timsort

Accessed November 29, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1363358981 · 0.5 · snapshot enwiki-001363358981

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners
  • Index — every corpus, paper and record
  • Estate guide — what a public data estate is
  • Founder — Lois-Kleinner Alpasan
  • Records — DOIs and external deposits
  • Documents — one-pagers, PDFs and QR codes
  • Brand kit — logos and share images
  • Papers — plain-language briefs with citations
  • Browser — search engine, PWA and bookmarklet
  • Anticloud — the operator and its projects

As seen in Business Insider · 336 outlets · evidence DOIs · plain-language papers

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.