Jump to content

Timsort: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Undid revision 381305127 by 80.169.172.178 (talk)
link to TIm Peters
Line 12: Line 12:
'''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 the [[Python (programming language)|Python programming language]], and has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in [[Java 7|Java SE 7]].<ref>http://hg.openjdk.java.net/jdk7/tl/jdk/rev/bfd7abda8f79</ref>
'''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 the [[Python (programming language)|Python programming language]], and has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in [[Java 7|Java SE 7]].<ref>http://hg.openjdk.java.net/jdk7/tl/jdk/rev/bfd7abda8f79</ref>


Tim Peters describes the algorithm as follows<ref>http://bugs.python.org/file4451/timsort.txt</ref>:
[[Tim Peters]] describes the algorithm as follows<ref>http://bugs.python.org/file4451/timsort.txt</ref>:


{{quote|[...] an adaptive, stable, natural mergesort, modestly called timsort (hey, I earned it &lt;wink&gt;). 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.
{{quote|[...] an adaptive, stable, natural mergesort, modestly called timsort (hey, I earned it &lt;wink&gt;). 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.

Revision as of 15:25, 7 September 2010

Timsort
Step-by-step visualisation of timsort
A visual representation of timsort. For short lists such as this, timsort is equivalent to insertion sort.
ClassSorting algorithm
Data structureArray
Worst-case performance
Best-case performance
Average performance
Worst-case space complexity
OptimalYes

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 the Python programming language, and has been Python's standard sorting algorithm since version 2.3. It is now also used to sort arrays in Java SE 7.[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 takes advantage of the fact that sublists of the data may already be in order or reverse order.[4]

See also

References

  1. ^ http://hg.openjdk.java.net/jdk7/tl/jdk/rev/bfd7abda8f79
  2. ^ http://bugs.python.org/file4451/timsort.txt
  3. ^ http://mail.python.org/pipermail/python-dev/2002-July/026837.html
  4. ^ Martelli, Alex (2006). Python in a Nutshell (In a Nutshell (O'Reilly)). O'Reilly Media, Inc. p. 57. ISBN 0596100469.

External links