Tim Sort Algorithm
In this article, we will discuss the Tim sort Algorithm. Tim-sort is a sorting algorithm derived from insertion sort and merge sort. It was designed to perform optimally on different kind of real-world data.
Tim sort is an adaptive sorting algorithm that needs O(n log n) comparisons to sort an array of n elements. It was designed and implemented by Tim Peters in 2002 in a python programming language. It has been python’s standard sorting algorithm since version 2.3. It is the fastest sorting algorithm.
The basic approach used in the Tim sort algorithm is – first sort small chunks by using the insertion sort and then merge all the big chunks using the merge function of the merge sort.
Now, let’s see the algorithm of Tim sort.
Algorithm
Working of Tim sort Algorithm
Now, let’s see the working of the Tim sort Algorithm.
In Tim sort, first the array is divided into small chunks that are known as RUN. After dividing, each individual RUN is taken, and sorted using the insertion sort technique. After that, all sorted RUNs are merged using the merge() function of the merge sort algorithm.
In Tim sort, the advantage of using the insertion sort is that insertion sort works efficiently for the array with small size.
Example of Tim sort Algorithm
Let’s see the example of Tim sort algorithm. To understand the working of Tim sort algorithm, let’s take an unsorted array. It will be easier to understand the Tim sort via an example.
Suppose the array elements are –
For the simple illustration, let’s consider the size of RUN is 4.
Now, divide the given array into two sub-arrays that are –
The first sub-array is –
Sorted subarray | Unsorted subarray | Array |
---|---|---|
(40) | (10, 20, 42) | (40, 10, 20, 42) |
First iteration
a[1] = 10
Sorted subarray | Unsorted subarray | Array |
---|---|---|
(10, 40) | (20, 42) | (10, 40, 20, 42) |
Second iteration
a[2] = 20
Sorted subarray | Unsorted subarray | Array |
---|---|---|
(10, 20, 40) | (42) | (10, 20, 40, 42) |
Third iteration
a[3] = 42
Sorted subarray | Unsorted subarray | Final array |
---|---|---|
(10, 20, 40, 42) | () | (10, 20, 40, 42) |
The second sub-array is –
Sorted subarray | Unsorted subarray | Final array |
---|---|---|
(27) | (25, 1, 19) | (27, 25, 1, 19) |
First iteration
a[1] = 25
Sorted subarray | Unsorted subarray | Final array |
---|---|---|
(25, 27) | (1, 19) | (25, 27, 1, 19) |
Second iteration
a[2] = 1
Sorted subarray | Unsorted subarray | Final array |
---|---|---|
(1, 25, 27) | (19) | (1, 25, 27, 19) |
Third iteration
a[3] = 19
Sorted subarray | Unsorted subarray | Final array |
---|---|---|
(1, 19, 25, 27) | () | (1, 19, 25, 27) |
Now, merge both sorted subarrays to get the final array as –
Now, the array is completely sorted.
Tim sort complexity
Now, let’s see the time complexity of Tim sort in the best case, average case, and worst case. We will also see the space complexity of Tim sort.
1. Time Complexity
Case | Time Complexity |
---|---|
Best Case | O(n) |
Average Case | O(n log n) |
Worst Case | O(n log n) |
- Best Case Complexity – It occurs when there is no sorting required, i.e. the array is already sorted. The best-case time complexity of Tim sort is O(n).
- Average Case Complexity – It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of Tim sort is O(n log n).
- Worst Case Complexity – It occurs when the array elements are required to be sorted in reverse order. That means suppose you have to sort the array elements in ascending order, but its elements are in descending order. The worst-case time complexity of Tim sort is O(n log n).
2. Space Complexity
Space Complexity | O(n) |
Stable | YES |
- The space complexity of Tim sort is O(n).
Implementation of Tim sort
Now, let’s see the programs of Tim sort in different programming languages.
Program: Write a program to implement Tim sort in C language.
Output
After the execution of the above code, the output will be –
Program: Write a program to implement Tim sort in C++.
Output
Program: Write a program to implement Tim sort in C#.
Output
Program: Write a program to implement Tim sort in Java.
Output
After the execution of above code, the output will be –
So, that’s all about the article. Hope the article will be helpful and informative to you.