Understanding Tim Sort Algorithm

amit220688 0 views 27 slides Sep 01, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

Tim Sort Sorting Algorithm with Applications. Introduced in 2002, it is a well-known sorting algorithm used in Java and Python.


Slide Content

CCE3101: Understanding Tim Sort Algorithm
Dr Amit Chaurasia
August 23, 2025

Introduction to Tim Sort
▶Tim Sort is a hybrid sorting algorithm, derived from Merge
Sort and Insertion Sort.
▶It is designed to perform well on many kinds of real-world data.
▶Tim Sort is the default sorting algorithm in Python's
sorted().

How Tim Sort Works
▶**Combination of Merge Sort and Insertion Sort:**
▶▶≪Ⓢ⋒®⋔⋌≫≪ ⪋≫®⋔≓ ⊴↰⋑⋌⋒≪⋔ ⋏≫® Ⓢ≎⌢≏≏ ⋓⌢⋔⌢ Ⓢ⋒⋔Ⓢ≲
▶△⋒®⋎⋒ ⪋≫®⋔≓ ⊴↰⋑⋌⋒≪⋔ ⋏≫® ≏⌢®⋎⋒ ⋓⌢⋔⌢Ⓢ⋒⋔Ⓢ ⌢≪⋓ ⋍≫®⫆Ⓢ ⋍⋒≏≏ ⋏≫®
merging.
▶**Key Steps in Tim Sort:**
1.
2.
3.

Tim Sort Pseudocode
The Tim Sort algorithm can be described as follows:
Data:
Result:
Step 1: Divide the array into blocks of size T.
for
Sort
end
Step 2: Merge the sorted blocks.
while
Merge the ørst two blocks into a temporary array.
if
Merge the temporary array with the next block.
end
end
return
Algorithm 1: Pseudocode of Tim Sort

Tim Sort Complexity Analysis
▶Best Case Complexity:
On
▶Worst Case Complexity:
On
▶Average Case Complexity:
On
▶Space Complexity:
On

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Tim Sort Example
Consider the array:
arr5,,,,,,,]
Step 1: Divide into Runs
(Assume block size of 4)
run15,,,],2,,,]
Step 2: Sort Runs using Insertion Sort
▶Sort run15,,,]
run11,,]
▶Sort run22,,,]
run22,,]
Step 3: Merge Sorted Runs
[1,,,,,,,]

Step 1: Divide the Array into Runs
Consider the array:
arr5,,,,,,,]
Step 1: Divide the Array into Runs
51872346Run 1Run 2

Step 2: Sort the Runs
Now let's sort the runs using Insertion Sort.
Sort Run 1: [5, 1, 8, 7]
51871578
Sort Run 2: [2, 3, 4, 6]
23462346

Step 3: Merge the Sorted Runs
After sorting the runs, we merge the two sorted runs.
Merge Run 1 and Run 2:
12345678
The ønal sorted
array is:
[1,,,,,,,]

Worst Case and Best Case
In the worst case, TimSort takesn
array of
is already sorted, it runs in linear time, meaning that it is an
adaptive sorting algorithm.
For an input that has
On. More strongly, the time isn, where the
run-length entropy iis
deøned to be:
H
r
X
i1
ni
n
log
2
ni
n

Run-Length Entropy
When all run sizes are equal, the run-length entropy
2r
maximum value for any given number
smaller when the runs have unevenly distributed sizes. The formula
for the running time is given as
nH
one.

Galloping Routine and Dual-Run Entropy
The above behavior regarding the run-length entropy
derived by TimSort's merge criteria, as that is the part responsible
for detecting already-sorted parts. The "galloping" routine exploits
a new property that can be described as the dual-run entropy

:
H

=
s
X
i1
ni
n
log
2
ni
n
where
sn

)
element moves andn

)

⇚⋋⌟ ⪌⋌≎ ⪋≫®⋔ ⋌Ⓢ ⊴↰⋑⋌⋒≪⋔
▶Adaptive: Tim Sort adapts to the input data, making it more
⋒↰⋑⋌⋒≪⋔ ⋍⋋⋒≪ ⋔⋋⋒ ⋓⌢⋔⌢ ⋌Ⓢ ⌜⌢®⋔⋌⌢≏≏⌟ Ⓢ≫®⋔⋒⋓≲
falls into the adaptive sort family if it takes advantage of
⋒⌞⋌Ⓢ⋔⋌≪⋎ ≫®⋓⋒® ⋌≪ ⋌⋔Ⓢ ⋌≪⌜∔⋔≲ ▶⋔ ⋐⋒≪⋒⇆⋔Ⓢ ⋏®≫≎ ⋔⋋⋒
⌜®⋒Ⓢ≫®⋔⋒⋓≪⋒ⓈⓈ ⋌≪ ⋔⋋⋒ ⋌≪⌜∔⋔ Ⓢ⋒⌝∔⋒≪⋑⋒ ⇊ ≫® ⌢ ≏⋌≎⋌⋔⋒⋓ ⌢≎≫∔≪⋔
≫⋏ ⋓⋌Ⓢ≫®⋓⋒® ⋏≫® ∽⌢®⋌≫∔Ⓢ ⋓⋒⇆≪⋌⋔⋌≫≪Ⓢ ≫⋏ ≎⋒⌢Ⓢ∔®⋒Ⓢ ≫⋏ ⋓⋌Ⓢ≫®⋓⋒® ⇊
and sorts faster. Adaptive sorting is usually performed by
modifying existing sorting algorithms.
▶Hybrid Approach: Combines the strengths of both Insertion
Sort (for small runs) and Merge Sort (for merging).
▶Optimized for Real-World Data: Tim Sort takes advantage of
existing ordered sequences in the data (runs).

Applications of Tim Sort
▶Tim Sort is the default sorting algorithm in Python's
sorted()Timsort has been Python’s standard
sorting algorithm since version 2.3, but
starting with 3.11 it uses
instead, a derived algorithm with a more
robust merge policy.).
▶⊴↰⋑⋌⋒≪⋔ ⋏≫® L⌢®⋎⋒ ⊵⌢⋔⌢Ⓢ⋒⋔Ⓢ≓ ⊴Ⓢ⌜⋒⋑⋌⌢≏≏⌟ ∔Ⓢ⋒⋏∔≏ ⋍⋋⋒≪ ⋓⌢⋔⌢ ⋌Ⓢ
partially sorted or nearly sorted.
▶Merging Large Datasets: Optimized for merging sorted blocks
or runs.

Tim Sort vs Other Sorting Algorithms
▶Merge Sort: Similar in approach but more memory intensive
because it requires extra space for merging.
▶⋚∔⋌⋑⫆ ⪋≫®⋔≓ △≫®⋒ ⋒↰⋑⋌⋒≪⋔ ⋌≪ ⋔⋋⋒ ⌢∽⋒®⌢⋎⋒ ⋑⌢Ⓢ⋒ ⋐∔⋔ ≏⋒ⓈⓈ Ⓢ⋔⌢⋐≏⋒
than Tim Sort.
▶Heap Sort: Not stable and not adaptive like Tim Sort.
Tim Sort is generally preferred when dealing with large, real-world
datasets due to its hybrid nature.

Conclusion
▶Tim Sort is a powerful hybrid sorting algorithm that optimizes
the merging process.
▶▶⋔ ⋌Ⓢ ⌢⋓⌢⌜⋔⋌∽⋒≜ ⋒↰⋑⋌⋒≪⋔≜ ⌢≪⋓ ⋍⋌⋓⋒≏⌟ ∔Ⓢ⋒⋓ ⋌≪ ⌢⌜⌜≏⋌⋑⌢⋔⋌≫≪Ⓢ ⋔⋋⌢⋔
require sorting large datasets.
▶⪌⋋⋒ ⌢≏⋎≫®⋌⋔⋋≎⪆Ⓢ ⋔⋌≎⋒ ⋑≫≎⌜≏⋒⌞⋌⋔⌟ ⋎∔⌢®⌢≪⋔⋒⋒Ⓢ ⋒↰⋑⋌⋒≪⋑⌟ ⋏≫® ⌢
variety of input data.

Questions?
Any Questions?