Open In App

Tim Sort in Python

Last Updated : 31 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Sorting is a fundamental operation in computer science, used in a variety of applications from data analysis to algorithms optimization. One such efficient and widely used sorting algorithm is Tim Sort. Created by Tim Peters in 2002, Tim Sort is the default sorting algorithm in Python and is renowned for its speed and efficiency in real-world data scenarios.

What is 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’s efficiency comes from its ability to exploit the structure present in the data, such as runs (consecutive sequences that are already ordered) and merges these runs using a modified merge sort approach.

How Tim Sort Works?

  1. Identify Runs: The algorithm first scans the list to identify runs, which are sequences of consecutive elements that are either ascending or descending. If a run is descending, Tim Sort reverses it to make it ascending.
  2. Use Insertion Sort: For small runs (default size 32 or less), Tim Sort uses insertion sort to order the elements. Insertion sort is efficient for small datasets and nearly sorted datasets.
  3. Merge Runs: Once the list is divided into sorted runs, Tim Sort uses a modified merge sort to merge these runs together, ensuring stability and efficiency.
  4. Galloping Mode: An optimization used during merging to quickly merge runs when one run’s elements are much larger than the other’s.

Why Tim Sort is Efficient?

  • Exploitation of Natural Runs: By identifying and using already sorted sequences, Tim Sort minimizes the number of comparisons and swaps.
  • Hybrid Approach: Combining insertion sort for small sequences and merge sort for larger sequences ensures efficiency.
  • Stability: Tim Sort maintains the relative order of equal elements, making it stable.

Tim Sort in Python:

Tim Sort is the default sorting algorithm in Python’s sort() method for lists and sorted() function. Here’s how you can use it:

Python
# Using the sort() method
my_list = [5, 3, 1, 4, 6, 2]
my_list.sort()
print("Sorted list using sort():", my_list)

# Using the sorted() function
my_list = [5, 3, 1, 4, 6, 2]
sorted_list = sorted(my_list)
print("Sorted list using sorted():", sorted_list)

Output
Sorted list using sort(): [1, 2, 3, 4, 5, 6]
Sorted list using sorted(): [1, 2, 3, 4, 5, 6]

Complete Implementation of Time Sort in Python:

The Tim Sort algorithm first divides the input array into smaller segments of size min_run and performs an insertion sort on each segment. It then starts merging the sorted segments, doubling the merge size in each iteration until the entire array is merged.

The key steps are:

  • Initialize the minimum run size (min_run) to 32.
  • Traverse the array in steps of min_run and perform insertion sort on each segment.
  • Start merging the sorted segments, doubling the merge size in each iteration.
  • Return the sorted array.

Implementation:

Python
def insertion_sort(arr, left=0, right=None):
    # Base case: if the array is already sorted, do nothing
    if right is None:
        right = len(arr) - 1

    # Iterate through the array, starting from the second element
    for i in range(left + 1, right + 1):
        # Select the current element
        key_item = arr[i]

        # Compare the current element with the previous one
        j = i - 1

        # While the previous element is greater than the current one,
        # shift the previous element to the next position
        while j >= left and arr[j] > key_item:
            arr[j + 1] = arr[j]
            j -= 1

        # Once the loop ends, the previous element is less than or equal to
        # the current element, so place the current element after it
        arr[j + 1] = key_item

    return arr


def merge(left, right):
    # If the left subarray is empty, return the right subarray
    if not left:
        return right

    # If the right subarray is empty, return the left subarray
    if not right:
        return left

    # Compare the first elements of the two subarrays
    if left[0] < right[0]:
        # If the first element of the left subarray is smaller,
        # recursively merge the left subarray with the right one
        return [left[0]] + merge(left[1:], right)
    else:
        # If the first element of the right subarray is smaller,
        # recursively merge the right subarray with the left one
        return [right[0]] + merge(left, right[1:])


def tim_sort(arr):
    # Initialize the minimum run size
    min_run = 32

    # Find the length of the array
    n = len(arr)

    # Traverse the array and do insertion sort on each segment of size min_run
    for i in range(0, n, min_run):
        insertion_sort(arr, i, min(i + min_run - 1, (n - 1)))

    # Start merging from size 32 (or min_run)
    size = min_run
    while size < n:
        # Divide the array into merge_size
        for start in range(0, n, size * 2):
            # Find the midpoint and endpoint of the left and right subarrays
            midpoint = start + size
            end = min((start + size * 2 - 1), (n - 1))

            # Merge the two subarrays
            merged_array = merge(arr[start:midpoint], arr[midpoint:end + 1])

            # Assign the merged array to the original array
            arr[start:start + len(merged_array)] = merged_array

        # Increase the merge size for the next iteration
        size *= 2

    return arr
  
  
 # Using the sorted() function
my_list = [5, 3, 1, 4, 6, 2]
sorted_list = tim_sort(my_list)
print("Sorted list using Tim Sort:", sorted_list)

Output
Sorted list using Tim Sort: [1, 2, 3, 4, 5, 6]

Time complexity: O(n log n) in the average and worst cases, making it an efficient sorting algorithm for large input arrays.
Auxiliary space: O(n) as the algorithm needs to create new arrays to store the merged results during the merge step.




Similar Reads

Tag sort or Bucket sort or Bin sort in Python
Tag sort, also known as Bucket sort or Bin sort, is a non-comparison based sorting algorithm that distributes elements of an array into a number of "buckets", and then sorts each bucket individually. Tag sort or Bucket sort or Bin sort Algorithm:Determine Range:Find the maximum and minimum values in the input array to determine the range of tags ne
2 min read
Comparison among Bubble Sort, Selection Sort and Insertion Sort
Bubble Sort, Selection Sort, and Insertion Sort are simple sorting algorithms that are commonly used to sort small datasets or as building blocks for more complex sorting algorithms. Here's a comparison of the three algorithms: Bubble Sort:Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when the input array is already
15 min read
C/C++ Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Java Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Insertion sort to sort even and odd positioned elements in different orders
We are given an array. We need to sort the even positioned elements in the ascending order and the odd positioned elements in the descending order. We must apply insertion sort to sort them.Examples: Input : a[] = {7, 10, 11, 3, 6, 9, 2, 13, 0} Output : 11 3 7 9 6 10 2 13 0 Even positioned elements after sorting int ascending order : 3 9 10 13 Odd
7 min read
Quick Sort vs Merge Sort
Quick sort is an internal algorithm which is based on divide and conquer strategy. In this: The array of elements is divided into parts repeatedly until it is not possible to divide it further.It is also known as “partition exchange sort”.It uses a key element (pivot) for partitioning the elements.One left partition contains all those elements that
4 min read
Sort an array of pairs using Java Arrays.sort() with custom Comparator
Given an array of pairs of integers. The task is to sort the array with respect to the second element of the pair. Examples: Input: [(1, 2), (3, 5), (2, 6), (1, 7)]Output: [(1, 2), (3, 5), (2, 6), (1, 7)] Input: [(10, 20), (20, 30), (5, 6), (2, 5)]Output: [(2, 5), (5, 6), (10, 20), (20, 30)] Approach: Store the pairs in an array using a user-define
2 min read
Sort an Array which contain 1 to N values in O(N) using Cycle Sort
Prerequisite: Cycle SortGiven an array arr[] of elements from 1 to N, the task is to sort the given array in O(N) time.Examples: Input: arr[] = { 2, 1, 5, 4, 3} Output: 1 2 3 4 5 Explanation: Since arr[0] = 2 is not at correct position, then swap arr[0] with arr[arr[0] - 1] Now array becomes: arr[] = {1, 2, 5, 4, 3}Now arr[2] = 5 is not at correct
6 min read
Add elements in start to sort the array | Variation of Stalin Sort
Stalin sort (also 'dictator sort' and 'trump sort') is a nonsensical 'sorting' algorithm in which each element that is not in the correct order is simply eliminated from the list. This sorting algorithm is a less destructive variation of Stalin sort, that will actually sort the list: In this case, the elements that are not in order are moved to the
6 min read
sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL
In this article, we will discuss the difference between sort(), partial_sort(), and nth_element()+sort(). Below is the illustration of the above functions: sort(): C++ STL provides a function sort() that sorts a list of element in O(N*log N) time. By default, sort() sorts an array in ascending order. Below is the program to illustrate sort(): // C+
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg