Back to All Algorithms
Tim Sort
Category: Sorting | Time: O(n log n) | Space: O(n)
Visualization
Visual representation of the data structure
About Tim Sort

A hybrid stable sorting algorithm, derived from merge sort and insertion sort, designed to perform well on many kinds of real-world data. It was invented by Tim Peters in 2002 for use in the Python programming language.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Variable State
{
  "n": 13
}
Tim Sort: How It Works
Last updated October 28, 2026

Overview

Tim Sort is a hybrid of Insertion Sort and Merge Sort, designed by Tim Peters for Python in 2002 and now used by Python, Java (for objects) and many other runtimes. Its idea is that real-world data often contains sorted stretches, so it sorts small chunks quickly and then merges them.

The simplified version on this page splits the array into runs of RUN = 32 elements, sorts each run with Insertion Sort, and then merges runs of size 32, 64, 128 and so on. Production Tim Sort goes further: it detects naturally occurring runs, reverses descending ones, and uses 'galloping' to speed up merges.

The default input has 13 elements, which is smaller than one run. The dry run therefore shows a single Insertion Sort pass and no merging — exactly what real Tim Sort does for small arrays. Paste in more than 32 numbers to see the merge phase.

Step by step

  1. Divide the array into consecutive runs of size RUN (the last run may be shorter).
  2. Sort each run with Insertion Sort.
  3. Set size = RUN and merge neighbouring runs of length size into runs of length 2 * size.
  4. Double size and repeat until one run covers the whole array.

Complexity

CaseCostWhy
Best caseO(n)Full Tim Sort detects that already-sorted input is one long run.
Average caseO(n log n)Insertion sort on fixed-size runs plus log(n/RUN) merge levels.
Worst caseO(n log n)Merge structure guarantees the bound.
Extra spaceO(n)Temporary arrays during merging.

When to use it

  • General-purpose stable sorting in standard libraries — you are probably already using it via sorted() in Python or Arrays.sort on objects in Java.
  • Data with existing order: logs appended in time order, lists that were sorted and then lightly edited.
  • Whenever stability and a guaranteed O(n log n) are both required.

Common mistakes

  • The fixed-run version shown here does not get the O(n) best case; that requires natural run detection.
  • A RUN size that is too large makes the insertion-sort phase quadratic in practice; 32–64 is the usual range.
  • Merging with < instead of <= loses stability, the property Tim Sort is chosen for.
Worked Example: Tim Sort Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Tim Sort: follow each row, predict the next one, and compare. The run below takes 170 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
5, 21, 7, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50
Result
return value = [-1,0,1,3,5,7,19,21,23,45,50,67,89]
StepLineArrayWhat happens
151[5, 21, 7, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50]n = 13
118[5, 21, 7, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50]j = 1, temp = 7, condition = 21 > 7
129[5, 21, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50]
1412[5, 7, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50]
228[5, 7, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50]j = 3, temp = 19, condition = 23 > 19
239[5, 7, 21, 23, 23, 1, 45, 67, 89, 3, 0, -1, 50]
258[5, 7, 21, 23, 23, 1, 45, 67, 89, 3, 0, -1, 50]j = 2, temp = 19, condition = 21 > 19
269[5, 7, 21, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50]
2812[5, 7, 19, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50]
328[5, 7, 19, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50]j = 4, temp = 1, condition = 23 > 1
339[5, 7, 19, 21, 23, 23, 45, 67, 89, 3, 0, -1, 50]
358[5, 7, 19, 21, 23, 23, 45, 67, 89, 3, 0, -1, 50]j = 3, temp = 1, condition = 21 > 1
369[5, 7, 19, 21, 21, 23, 45, 67, 89, 3, 0, -1, 50]
388[5, 7, 19, 21, 21, 23, 45, 67, 89, 3, 0, -1, 50]j = 2, temp = 1, condition = 19 > 1
399[5, 7, 19, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]
418[5, 7, 19, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]j = 1, temp = 1, condition = 7 > 1
429[5, 7, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]
448[5, 7, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]j = 0, temp = 1, condition = 5 > 1
459[5, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]
4712[1, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]
638[1, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50]j = 8, temp = 3, condition = 89 > 3
649[1, 5, 7, 19, 21, 23, 45, 67, 89, 89, 0, -1, 50]
668[1, 5, 7, 19, 21, 23, 45, 67, 89, 89, 0, -1, 50]j = 7, temp = 3, condition = 67 > 3
679[1, 5, 7, 19, 21, 23, 45, 67, 67, 89, 0, -1, 50]
698[1, 5, 7, 19, 21, 23, 45, 67, 67, 89, 0, -1, 50]j = 6, temp = 3, condition = 45 > 3
709[1, 5, 7, 19, 21, 23, 45, 45, 67, 89, 0, -1, 50]
728[1, 5, 7, 19, 21, 23, 45, 45, 67, 89, 0, -1, 50]j = 5, temp = 3, condition = 23 > 3
739[1, 5, 7, 19, 21, 23, 23, 45, 67, 89, 0, -1, 50]
758[1, 5, 7, 19, 21, 23, 23, 45, 67, 89, 0, -1, 50]j = 4, temp = 3, condition = 21 > 3
769[1, 5, 7, 19, 21, 21, 23, 45, 67, 89, 0, -1, 50]
788[1, 5, 7, 19, 21, 21, 23, 45, 67, 89, 0, -1, 50]j = 3, temp = 3, condition = 19 > 3
799[1, 5, 7, 19, 19, 21, 23, 45, 67, 89, 0, -1, 50]
818[1, 5, 7, 19, 19, 21, 23, 45, 67, 89, 0, -1, 50]j = 2, temp = 3, condition = 7 > 3
829[1, 5, 7, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50]
848[1, 5, 7, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50]j = 1, temp = 3, condition = 5 > 3
859[1, 5, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50]
8712[1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50]
918[1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50]j = 9, temp = 0, condition = 89 > 0
929[1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 89, -1, 50]
948[1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 89, -1, 50]j = 8, temp = 0, condition = 67 > 0

The remaining 47 key steps follow the same pattern. Press play in the visualizer to watch every step through to the result.

Frequently Asked Questions
About Tim Sort

What is Tim Sort?

Tim Sort is a highly efficient, adaptive, and stable hybrid sorting algorithm, derived from Merge Sort and Insertion Sort. It is designed to perform well on many kinds of real-world data. It is the standard sorting algorithm in Python and Java (for objects).

How does Tim Sort work?

It works by finding 'natural runs' (contiguous sorted subsequences) in the data. For runs that are smaller than a certain threshold (minrun), it uses Insertion Sort to extend them. Then, it merges these sorted runs using a highly optimized Merge Sort, which employs techniques like 'galloping' to speed up the merge process.

What is the time complexity of Tim Sort?

Tim Sort has a best-case time complexity of O(n) when the input is already sorted. Its average and worst-case time complexity are both O(n log n), making it very efficient and reliable.

Why is Tim Sort so popular in practice?

It's highly efficient because it's adaptive. It takes advantage of the fact that real-world data is often partially sorted with existing 'runs'. Its use of Insertion Sort for small segments and its intelligent merging strategy make it faster than a pure Merge Sort for most practical datasets.

What is a 'minrun' in Tim Sort?

A 'minrun' is a minimum size for a 'run' (a sorted subsequence). In TimSort, if a natural run is shorter than the minrun, it's extended using Insertion Sort until it reaches that minimum size. This helps to ensure that the later merging process is balanced and efficient. The minrun size is calculated based on the size of the total array, typically between 32 and 65.

What is 'galloping' in Tim Sort's merge step?

Galloping is an optimization used during the merge step. When data from one run is consistently winning against the other, the algorithm enters 'galloping mode'. It uses an exponential search (gallop) to quickly find where the next element from the other run should be placed, skipping over large chunks of data.

Is Tim Sort an in-place algorithm?

It is not strictly in-place. It aims to be space-efficient. For the merge step, it requires temporary storage. The size of this temporary space is at most N/2 in the worst case, but often much smaller (e.g., the size of the smaller run being merged).

Why does Tim Sort use Insertion Sort?

Insertion Sort is used because it is extremely fast for small arrays (due to low overhead) and for arrays that are nearly sorted. Both of these conditions are frequently met by the 'runs' that Tim Sort processes.

How does Tim Sort maintain stability?

It maintains stability because both of its component algorithms, Insertion Sort and its specific implementation of Merge Sort, are stable. During the merge, if elements are equal, the element from the run that appeared first in the original array is always chosen first.

Can Tim Sort's performance degrade?

Unlike Quick Sort, Tim Sort does not have a common worst-case input that degrades its performance to O(n^2). Its performance is consistently O(n log n) due to its merge-based strategy. The main performance variations come from how much existing order it can exploit in the input data.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.