VOOZH about

URL: https://www.geeksforgeeks.org/dsa/tim-sort-in-python/

⇱ Tim Sort in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Tim Sort in Python

Last Updated : 26 Feb, 2025

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. It was 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.

How Tim Sort Works?

Let’s consider the following array as an example: arr[] = {4, 2, 8, 6, 1, 5, 9, 3, 7}.

Step 1: Define the size of the run

  • Minimum run size: 32 (we’ll ignore this step since our array is small)

Step 2: Divide the array into runs

  • In this step, we’ll use insertion sort to sort the small subsequences (runs) within the array.
  • The initial array: [4, 2, 8, 6, 1, 5, 9, 3, 7]
  • No initial runs are present, so we’ll create runs using insertion sort.
  • Sorted runs: [2, 4], [6, 8], [1, 5, 9], [3, 7]
  • Updated array: [2, 4, 6, 8, 1, 5, 9, 3, 7]

Step 3: Merge the runs

  • In this step, we’ll merge the sorted runs using a modified merge sort algorithm.
  • Merge the runs until the entire array is sorted.
  • Merged runs: [2, 4, 6, 8], [1, 3, 5, 7, 9]
  • Updated array: [2, 4, 6, 8, 1, 3, 5, 7, 9]

Step 4: Adjust the run size

  • After each merge operation, we double the size of the run until it exceeds the length of the array.
  • The run size doubles: 32, 64, 128 (we’ll ignore this step since our array is small)

Step 5: Continue merging

  • Repeat the merging process until the entire array is sorted.
  • Final merged run: [1, 2, 3, 4, 5, 6, 7, 8, 9]

The final sorted array is [1, 2, 3, 4, 5, 6, 7, 8, 9].

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.

Implementation:

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


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.

Code Example:


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.

Comment
Article Tags: