Language Implementations

What Sorting Algorithm Does Python Use?

Updated June 8, 2026 5 min read

If you have ever wondered what happens when you call sorted() or list.sort() in Python, the answer is Tim Sort — a hybrid algorithm invented for Python in 2002 that is now used across the software world.

Tim Sort, built for Python

Tim Peters designed Tim Sort specifically for CPython. It combines Merge Sort and Insertion Sort, detecting pre-sorted 'runs' in the data and merging them efficiently. It is stable, runs in O(n log n) worst case, and reaches O(n) on already-sorted input. Read the full Tim Sort explainer.

Why Python chose it

Real-world data is rarely random, and Python needed a stable, predictable default that performs well on partially-ordered input. Tim Sort fits perfectly, which is why it spread to Java, JavaScript, Swift, and Android too.

Frequently asked questions

What sorting algorithm does Python use? +
Python uses Tim Sort for both sorted() and list.sort(). It is a stable hybrid of Merge Sort and Insertion Sort with O(n log n) worst case and O(n) best case on sorted data.
Is Python's sort stable? +
Yes. Tim Sort is stable, so elements that compare equal keep their original relative order — which is essential for correct multi-key sorting.

See it in motion

Watch this algorithm and 9 others run step by step in our free interactive visualizer.

▶ Launch Visualiser

Related articles

← Back to all articles