GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

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. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsequences of the data that are already ordered (runs) and uses them to sort the remainder more efficiently. This is done by merging runs until certain criteria are fulfilled. 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. Timsort is also used to sort arrays of non-primitive type in Java SE 7, on the Android platform, in GNU... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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. It was implemented by Tim Peters in 2002 for use in the Python programming language. The algorithm finds subsequences of the data that are already ordered (runs) and uses them to sort the remainder more efficiently. This is done by merging runs until certain criteria are fulfilled. 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. 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. Rust used a custom version of Timsort until May 2024. The galloping technique derives from Carlsson, Levcopoulos, and O. Petersson's 1990 paper "Sublinear merging and natural merge sort" and Peter McIlroy's 1993 paper "Optimistic Sorting and Information Theoretic Complexity". An individual merge of runs R1 and R2 keeps the count of consecutive elements selected from a run. 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. Let us assume that R1 is responsible for triggering it. 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. 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. a region of uncertainty comprising 2 k−1 − 1 consecutive elements of R1. The second stage performs a straight binary search of this region to find the exact location in R1 for x. Galloping mode is an attempt to adapt the merge algorithm to the pattern of intervals between elements in runs. Galloping is not always efficient. In some cases galloping mode requires more comparisons than a simple linear search. 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. This implies an initial threshold of 7. 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. (2) The success or failure of galloping is used to adjust min_gallop. 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. Otherwise, the value is incremented by one, thus discouraging a return to galloping mode. In the case of random data, the value of min_gallop becomes so large that galloping mode never recurs. \Eta=-\sum_{i=1}^r \frac{n_i}{n}\log_2 \frac{n_i}{n}. 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. 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. 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. 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}. where s the number of times "galloping" is interrupted by and s \leq r . It can be proven that TimSort takes up to O(n+n\Eta) element moves and O(n+n\Eta^{*}) value comparisons.

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Timsort". 1783614567. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1363358981

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·