What algorithm does python's sorted() use?

Question:

In Python 2.7, how does Python’s built-in sorted function work – what algorithm does it use?

Asked By: Hartley Brody

||

Answers:

Python uses an algorithm called Timsort:

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. The algorithm finds subsets of the data that are
already ordered, and uses the subsets to sort the data more
efficiently. This is done by merging an identified subset, called a
run, with existing runs until certain criteria are fulfilled. Timsort
has been Python’s standard sorting algorithm since version 2.3. It is
now also used to sort arrays in Java SE 7, and on the Android
platform.

Answered By: bgporter

The sort algorithm is called Timsort. See timsort

Answered By: Pierce

Since 2.3 Python has used timsort.

More info: http://bugs.python.org/file4451/timsort.txt

Answered By: David Haynes
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.