What Sorting Algorithm Does Python Use?
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? +
Is Python's sort stable? +
See it in motion
Watch this algorithm and 9 others run step by step in our free interactive visualizer.
▶ Launch Visualiser