EN
Back to the archive

The encyclopedia · Engineering & Operations · Technical decision · 2002-2003

Timsort sorted real-world data by exploiting the order already in it

Tim Peters built Python's sort around natural runs, cutting comparisons on real lists to near N — and Java, Android and V8 copied it.

Python Software Foundation

the move

Sorting algorithms are usually measured on random data, but real lists are rarely random — databases, user interfaces and logs often arrive in nearly sorted chunks. Tim Peters, a Python core developer, designed timsort in 2002 for Python 2.3 on exactly that observation: an adaptive, stable, natural mergesort that finds the runs of order already present and exploits them.

The algorithm walks the array once, left to right, identifying ascending or descending runs; each run is pushed on a stack, and runs are merged whenever they satisfy a size-balance criterion so merges stay cheap. Runs shorter than a minimum length are topped up with insertion sort, which is fast on small arrays. Because the input is never split blindly, partially ordered data can be sorted with as few as N-1 comparisons.

The result replaced Python's previous samplesort hybrid and shipped as the standard list.sort() from Python 2.3. Its practical speed on real-world data made it an export: Java SE 7 adopted it for sorting arrays of objects, Android and GNU Octave use it, the V8 JavaScript engine adopted it, and Swift's sort and Rust's standard library were built on the same idea.

Timsort is stable — equal elements keep their relative order — so it underpins multi-key sorts such as sorting by zip code and then by name. Even a subtle bug found in 2015 in the standard implementations was fixed across Python, Java and Android, evidence of how widely the algorithm had spread.

why it works

  • Natural runs mean the data does part of the work before the sort starts
  • A merge-stack criterion keeps merges balanced and comparisons near optimal
  • Insertion sort tops up short runs, where it beats mergesort
  • Stability makes it usable for multi-key sorting pipelines
the payoffMerge the runs the data already containsclever

what transfers

Profile the real input, not the worst case: if the data arrives with structure, an algorithm that harvests that structure beats a theoretically optimal one on real workloads.

what came after

Timsort remained Python's default sort until Python 3.11 replaced it with Powersort, a descendant with a more robust merge policy, and it still ships in Java, Android, V8 and Swift. The 2015 bug, which could crash Java and Android on certain large inputs, was patched across the ecosystem — a sign of how much real software now depends on one engineer's 2002 design.

references

spotted an error? The archive wants to know.

same kind of clever