Jump to content

Timsort

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 62.189.26.100 (talk) at 17:27, 12 November 2009 (timsort uses less than log(n!) comparisons, not log(n) comparisons - you can't even look at every observation in log(n) time). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Timsort
Step-by-step visualisation of timsort
Timsort in action on a list of numbers. For short lists of numbers like this one, Timsort is equivalent to Insertion Sort
ClassSorting algorithm
Data structureArray
Worst-case performance
Best-case performance
Average performance
Worst-case space complexity
OptimalSometimes

Timsort is a hybrid sorting algorithm derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was invented by Tim Peters in 2002, for use in versions 2.3 and later of the Python programming language, and is also used in Java as well [1].

Tim Peters describes the algorithm as follows [2]:

[...] an adaptive, stable, natural mergesort, modestly called

timsort (hey, I earned it <wink>). It has supernatural performance on many kinds of partially ordered arrays (less than lg(N!) comparisons needed, and as few as N-1), yet as fast as Python's previous highly tuned samplesort hybrid on random arrays.

In a nutshell, the main routine marches over the array once, left to right, alternately identifying the next run, then merging it into the previous runs "intelligently". Everything else is complication for speed, and some

hard-won measure of memory efficiency.

Like merge sort, it is a stable comparison sort with a worst-case time complexity of [3].

According to information theory, no comparison sort can perform fewer than comparisons in the average case, which requires that comparison sorting takes time on random data. However, on real-world data, Timsort often requires far fewer than comparisons, because it exploits the fact that sublists of the data may already be in order or reverse order.[4]

See also



References