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()andlist.sort()use Timsort. - Java’s
Arrays.sort()uses Timsort for arrays of objects, and a dual-pivot quicksort for arrays of primitive values such asint. 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.