VOOZH about

URL: https://www.geeksforgeeks.org/dsa/timsort/

⇱ TimSort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

TimSort

Last Updated : 30 Jan, 2026

TimSort is a hybrid sorting algorithm that uses the ideas of Merge Sort and Insertion Sort.

  • Used as the default sorting algorithm in Python (sorted(), list.sort()) and Java (from Java 7 onwards for Arrays.sort() on objects).
  • The key idea behind Timsort is to identify small sorted segments of the array, called runs, and then merge them efficiently to form the fully sorted array.

How Timsort Works

Timsort works in three main steps by combining ideas from Merge Sort and Insertion Sort:

  • Identify Runs: It scans the array to find small segments that are already sorted, called runs. If a run is in descending order, it’s reversed to make it ascending.
  • Sort Small Runs: If a run is shorter than a fixed size (usually 32), it is sorted using Insertion Sort, which is fast for small or nearly sorted data.
  • Merge Runs: Finally, Timsort merges these runs using rules that keep merging balanced and efficient, similar to Merge Sort, but optimized for real-world data.



Output
1 2 3 5 7 10 19 21 23 

Complexity Analysis of Tim Sort:

Time Complexity:

  • Best Case: Ω(n), when the array is already sorted.
  • Average Case: Θ(n log n), when the array is randomly ordered.
  • Worst Case: O(n log n), when the array is sorted in reverse order.

Auxiliary Space: O(n), additional space is required for merging runs.

Stability: Yes, Tim Sort is a stable sorting algorithm (maintains the relative order of equal elements).

Understanding Time Complexity For Tim Sort:

Timsort’s time complexity comes from the combination of two phases — sorting small runs using Insertion Sort and merging those runs using a Merge Sort like process.

Sorting Part: Detecting the runs takes linear time, O(n). Each run is then sorted using Insertion Sort, which takes O(k²) for a run of size k. Since there are about n/k such runs, the total cost for sorting all runs becomes O(n*k), which simplifies to O(n) because k is a small constant.

Merging Part: Merging two runs of total length m takes O(m) time. Every element in the array participates in each level of merging once, so each merge level costs O(n). As runs double in size after every merge, the number of merge levels is approximately log(n/k). Hence, the total merging cost becomes O(n log(n/k)), which simplifies to O(n log n).

Combining both steps gives the overall time complexity: sorting runs O(n) + merging runs O(n log n) = O(n log n).

Applications of Tim Sort:

  • Used as the default sorting algorithm in Python (sorted(), list.sort()) and Java (from Java 7 onwards for Arrays.sort() on objects).
  • Particularly effective for real-world datasets where data is often partially sorted.
  • Suitable for sorting large datasets where stability is required.
  • Performs well in database systems and search engines where maintaining order of equal keys matters.
  • Used in Android, Swift, and other libraries due to its stability and efficiency.

Advantages and Disadvantages of Tim Sort:

Advantages:

  • Stability: Tim Sort is a stable sorting algorithm, so equal elements maintain their original order.
  • Adaptive: Performs better on partially sorted data, often achieving close to O(n) performance.
  • Guaranteed Worst-Case Performance: Always bounded by O(n log n), making it reliable.
  • Practical Efficiency: Combines insertion sort (efficient on small runs) and merge sort (efficient on large data), making it faster for real-world use cases.

Disadvantages:

  • Space Complexity: Requires additional memory O(n) during merging, making it not strictly in-place.
  • Implementation Complexity: More complex to implement compared to simpler algorithms like Quick Sort or Merge Sort.
  • Not Cache-Friendly: Like merge sort, it may be slower in low-memory systems compared to in-place algorithms like Quick Sort.

Complexity Comparison with Merge and Quick Sort:

Algorithm

Time Complexity

Best

Average

Worst

Quick Sort

Ω(n*log(n))

θ(n*log(n))

O(n^2)

Merge Sort

Ω(n*log(n))

θ(n*log(n))

O(n*log(n))

Tim Sort

Ω(n)

θ(n*log(n))

O(n*log(n))

Comment
Article Tags: