Sorting Efficiency

Is Timsort Faster Than Quicksort?

On random data, Timsort is usually not faster than quicksort. On data with existing runs, it can be faster, and its worst case is O(n log n), unlike quicksort's O(n²).

On this page
  1. Short answer
  2. How each algorithm works
  3. Random data
  4. Data that is already partly ordered
  5. Worst-case guarantees
  6. Stability and memory
  7. Which languages use which
  8. Which to pick

Short answer

It depends on the data. On random data, Timsort is usually not faster than a well-written quicksort. On data that has runs of items already in order, which is common in real-world data, Timsort can be faster, and on input that is already sorted it runs in linear time. Quicksort has an O(n²) worst case, while Timsort’s worst case is O(n log n).

How each algorithm works

Quicksort picks a pivot, moves the items smaller than the pivot to one side and the larger items to the other, then sorts each side. Most of its work happens in that partitioning step.

Timsort looks for runs, which are stretches of items that are already in ascending order. A strictly descending run is reversed so that it becomes ascending. Short runs are extended to a minimum length, typically 32 to 64 items, using insertion sort. The runs are then merged, following rules that keep the merges balanced.

Random data

Random data has few runs for Timsort to use, so it falls back to its ordinary O(n log n) behavior. Quicksort’s partitioning is simple and works well with the memory cache when sorting arrays of plain numbers. That is one reason quicksort-style sorts are often fast on random arrays, and it is why they remain common for primitive values.

Data that is already partly ordered

Real data often has structure. A log that is appended in time order, or a list in which only a few items are out of place, contains long runs. Timsort merges those runs and makes far fewer comparisons than it would on random data. Quicksort benefits less from this kind of order, although a good pivot choice helps.

Worst-case guarantees

Timsort’s worst case is O(n log n). Plain quicksort’s worst case is O(n²), which happens when the pivot choices are consistently poor. Many libraries guard against this by switching to another algorithm when recursion goes too deep, so the practical risk is lower, but the guarantee is weaker than Timsort’s.

Stability and memory

Timsort is stable, so items with equal keys keep their original order. Standard quicksort is not stable. Timsort needs extra memory for its merges, and that memory can grow to O(n) in the worst case. Quicksort usually sorts in place, needing only a small amount of extra memory for recursion.

Which languages use which

  • Python’s sorted() and list.sort() use Timsort.
  • Java’s Arrays.sort() uses Timsort for arrays of objects, and a dual-pivot quicksort for arrays of primitive values such as int. For objects, Timsort’s stability and its handling of existing order are useful, and the cost of following object references makes quicksort’s cache advantage smaller.

Which to pick

If your data is mostly plain numbers in no particular order, a quicksort-style sort is a good fit, and a library’s default is usually a safe choice. If your data has existing order, or if you need equal items to keep their order, Timsort is a strong choice. For more on stability, see which sorting algorithms are stable and why.

The complete guide to sorting efficiency

What Is the Most Efficient Sorting Algorithm?
Read the guide

Keep exploring

Guides from other topics

All topics →

Big O for Data Structures

A practical guide to Big O for data structures: how arrays, lists, hash tables, trees and heaps compare on search, insert and delete costs, with examples.

Big O Notation 5 min read