Overview
Tim Sort scans for ascending or descending runs, extends short runs, and merges them while maintaining stack invariants that keep merging efficient and stable.
Sorts by detecting existing ordered runs and merging them with safeguards for real-world data.
Compact metadata for implementation and discovery.
Tim Sort scans for ascending or descending runs, extends short runs, and merges them while maintaining stack invariants that keep merging efficient and stable.
Quick facts
Runtime and memory behavior at a glance.
Best
O(n)
Average
O(n log n)
Worst
O(n log n)
Space
O(n)
Show existing ordered runs being detected and merged.
Tim Sort is designed for practical library sorting where partially ordered inputs are common.
What each reference supports for this entry.
Implementation reference for Timsort runs, minrun selection, and galloping merges.
Open sourceGood next jumps from the relationship graph, ranked for useful discovery.
TimSort merges detected runs using merge-sort-style stable merging.