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.
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
{
"n": 13
}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
- Divide the array into consecutive runs of size
RUN(the last run may be shorter). - Sort each run with Insertion Sort.
- Set
size = RUNand merge neighbouring runs of lengthsizeinto runs of length2 * size. - Double
sizeand repeat until one run covers the whole array.
Complexity
| Case | Cost | Why |
|---|---|---|
| Best case | O(n) | Full Tim Sort detects that already-sorted input is one long run. |
| Average case | O(n log n) | Insertion sort on fixed-size runs plus log(n/RUN) merge levels. |
| Worst case | O(n log n) | Merge structure guarantees the bound. |
| Extra space | O(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 orArrays.sorton 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.
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]
| Step | Line | Array | What happens |
|---|---|---|---|
| 1 | 51 | [5, 21, 7, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50] | n = 13 |
| 11 | 8 | [5, 21, 7, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50] | j = 1, temp = 7, condition = 21 > 7 |
| 12 | 9 | [5, 21, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50] | |
| 14 | 12 | [5, 7, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50] | |
| 22 | 8 | [5, 7, 21, 23, 19, 1, 45, 67, 89, 3, 0, -1, 50] | j = 3, temp = 19, condition = 23 > 19 |
| 23 | 9 | [5, 7, 21, 23, 23, 1, 45, 67, 89, 3, 0, -1, 50] | |
| 25 | 8 | [5, 7, 21, 23, 23, 1, 45, 67, 89, 3, 0, -1, 50] | j = 2, temp = 19, condition = 21 > 19 |
| 26 | 9 | [5, 7, 21, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50] | |
| 28 | 12 | [5, 7, 19, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50] | |
| 32 | 8 | [5, 7, 19, 21, 23, 1, 45, 67, 89, 3, 0, -1, 50] | j = 4, temp = 1, condition = 23 > 1 |
| 33 | 9 | [5, 7, 19, 21, 23, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 35 | 8 | [5, 7, 19, 21, 23, 23, 45, 67, 89, 3, 0, -1, 50] | j = 3, temp = 1, condition = 21 > 1 |
| 36 | 9 | [5, 7, 19, 21, 21, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 38 | 8 | [5, 7, 19, 21, 21, 23, 45, 67, 89, 3, 0, -1, 50] | j = 2, temp = 1, condition = 19 > 1 |
| 39 | 9 | [5, 7, 19, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 41 | 8 | [5, 7, 19, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | j = 1, temp = 1, condition = 7 > 1 |
| 42 | 9 | [5, 7, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 44 | 8 | [5, 7, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | j = 0, temp = 1, condition = 5 > 1 |
| 45 | 9 | [5, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 47 | 12 | [1, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | |
| 63 | 8 | [1, 5, 7, 19, 21, 23, 45, 67, 89, 3, 0, -1, 50] | j = 8, temp = 3, condition = 89 > 3 |
| 64 | 9 | [1, 5, 7, 19, 21, 23, 45, 67, 89, 89, 0, -1, 50] | |
| 66 | 8 | [1, 5, 7, 19, 21, 23, 45, 67, 89, 89, 0, -1, 50] | j = 7, temp = 3, condition = 67 > 3 |
| 67 | 9 | [1, 5, 7, 19, 21, 23, 45, 67, 67, 89, 0, -1, 50] | |
| 69 | 8 | [1, 5, 7, 19, 21, 23, 45, 67, 67, 89, 0, -1, 50] | j = 6, temp = 3, condition = 45 > 3 |
| 70 | 9 | [1, 5, 7, 19, 21, 23, 45, 45, 67, 89, 0, -1, 50] | |
| 72 | 8 | [1, 5, 7, 19, 21, 23, 45, 45, 67, 89, 0, -1, 50] | j = 5, temp = 3, condition = 23 > 3 |
| 73 | 9 | [1, 5, 7, 19, 21, 23, 23, 45, 67, 89, 0, -1, 50] | |
| 75 | 8 | [1, 5, 7, 19, 21, 23, 23, 45, 67, 89, 0, -1, 50] | j = 4, temp = 3, condition = 21 > 3 |
| 76 | 9 | [1, 5, 7, 19, 21, 21, 23, 45, 67, 89, 0, -1, 50] | |
| 78 | 8 | [1, 5, 7, 19, 21, 21, 23, 45, 67, 89, 0, -1, 50] | j = 3, temp = 3, condition = 19 > 3 |
| 79 | 9 | [1, 5, 7, 19, 19, 21, 23, 45, 67, 89, 0, -1, 50] | |
| 81 | 8 | [1, 5, 7, 19, 19, 21, 23, 45, 67, 89, 0, -1, 50] | j = 2, temp = 3, condition = 7 > 3 |
| 82 | 9 | [1, 5, 7, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50] | |
| 84 | 8 | [1, 5, 7, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50] | j = 1, temp = 3, condition = 5 > 3 |
| 85 | 9 | [1, 5, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50] | |
| 87 | 12 | [1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50] | |
| 91 | 8 | [1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 0, -1, 50] | j = 9, temp = 0, condition = 89 > 0 |
| 92 | 9 | [1, 3, 5, 7, 19, 21, 23, 45, 67, 89, 89, -1, 50] | |
| 94 | 8 | [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.