Skip to content

Instantly share code, notes, and snippets.

@NightStrang6r
Created May 31, 2024 18:30
Show Gist options
  • Save NightStrang6r/cd88b10faaf1583736cbe0675e09079a to your computer and use it in GitHub Desktop.
Save NightStrang6r/cd88b10faaf1583736cbe0675e09079a to your computer and use it in GitHub Desktop.

⚡ Timsort ⚡

🏆 The fastest sorting algorithm you've never heard of 🏆

Introduction

Timsort is a very fast, O(n * log(n)), stable sorting algorithm designed for the real world data, not academic purposes. Tim Peters created Timsort for Python in 2001.

Timsort first analyzes the list it is trying to sort and chooses the best approach based on that. Since its introduction, it has been used as the default sorting algorithm in Python, Java, the Android platform, and GNU Octave.

💫 Algorithmic Details (Why Timsort is so FAST?)

Before delving into Timsort, it's important to understand merge sort, one of its foundational components.

🍃 Merge Sort

Merge sort is a divide-and-conquer algorithm that works as follows:

  1. Divide: split the array into two halves.
  2. Conquer: recursively sort the two halves.
  3. Combine: merge the two sorted halves to produce the sorted array.

Merge sort operates with a time complexity of O(n * log(n)) and requires additional space proportional to the size of the input array, making its space complexity O(n).

🎆 Insertion Sort

The other component of Timsort is insertion sort, which is straightforward but inefficient for large datasets. Insertion sort works by iteratively taking an element from the unsorted portion and inserting it into the correct position in the sorted portion. Its time complexity is (O(n^2)), but for small datasets, it can be quite efficient.

⚡ Timsort

Timsort optimizes performance by combining the strengths of merge sort and insertion sort. The key steps of Timsort are:

  1. Identify Runs: a "run" is a subsequence of the array that is already ordered (either ascending or descending). Timsort scans the array to identify these runs.
  2. Sort Runs: use insertion sort to sort runs if they are smaller than a predefined threshold (typically 32).
  3. Merge Runs: merge the runs together using a merge sort process. This step is optimized to minimize the number of comparisons and the amount of data movement.

🔻 Pseudocode for Timsort:

def timsort(arr):
    min_run = calculate_min_run(len(arr))
    runs = []
    
    for start in range(0, len(arr), min_run):
        end = min(start + min_run, len(arr))
        run = arr[start:end]
        insertion_sort(run)
        runs.append(run)
    
    while len(runs) > 1:
        runs.append(merge(runs.pop(0), runs.pop(0)))
    
    return runs[0]

🔻 Pseudocode for calculate_min_run function:

def calculate_min_run(n):
    r = 0
    while n >= 64:
        r |= n & 1
        n >>= 1
    return n + r

❇️ Algorithmic Complexity

Timsort has a worst-case time complexity of O(n * log(n)), similar to merge sort. However, its best-case (O(n)) and average-case complexities can be significantly better due to the following factors:

  1. Natural Runs: by exploiting naturally occurring runs, Timsort can reduce the number of merge operations, making it faster on partially sorted data.
  2. Insertion Sort: for small arrays, insertion sort is used, which is more efficient than merge sort in this context.

♒️ Space Complexity

The space complexity of Timsort is O(n), primarily due to the additional storage required for merging runs.

☑️ Practical Uses

Timsort is widely used in various programming languages and platforms due to its efficiency on real-world data. Some notable uses include:

  1. Python: the default sorting algorithm in Python's built-in sort() method. 🐍
  2. Java: used in the java.util.Arrays.sort() method for objects. ☕
  3. Android: employed in the Android platform for sorting. 📱

Timsort is particularly effective for applications where data often exhibits patterns, such as partially sorted data or data with repeated elements. Examples include:

  • Data Analysis: where datasets may already be partially ordered. 📊
  • Database Systems: for optimizing query results that involve sorting operations. 🗄️
  • File Systems: where directory listings and file attributes may often be in a semi-sorted order. 🗃️

🌓 Comparison with Merge Sort

🎆 Performance

While both Timsort and merge sort have the same worst-case time complexity, Timsort tends to outperform merge sort on real-world data due to its optimization for natural runs and the use of insertion sort for small arrays. This makes Timsort faster in practice for many types of data commonly encountered in software applications.

🍵 Stability

Both Timsort and merge sort are stable sorting algorithms, meaning that they preserve the relative order of equal elements. This stability is crucial in scenarios where the order of equivalent elements carries significance, such as in sorting records by multiple fields.

📶 Space Efficiency

Although Timsort and merge sort both have O(n) space complexity, Timsort often requires less additional memory in practice due to its ability to work with runs directly in the array.

🐣 Complexity

In terms of complexity, both algorithms have the same worst-case time complexity, O(n*log(n)). However, Timsort's ability to leverage insertion sort and exploit natural runs allows it to perform better in many practical scenarios.

✔️ Conclusion

Timsort is a sophisticated sorting algorithm that effectively combines the strengths of merge sort and insertion sort. Its design, which exploits natural runs and uses insertion sort for small segments, makes it particularly efficient for real-world data. With a worst-case time complexity of O(n*log(n)) and practical optimizations that enhance its average-case performance, Timsort is a powerful tool for sorting operations in many programming environments.

By comparing Timsort with merge sort, it becomes evident that while both algorithms are theoretically similar in complexity, Timsort's practical optimizations make it superior in many cases. This has led to its widespread adoption in various programming languages and systems, underlining its importance as a modern sorting algorithm.

🌈 References

  1. Wikipedia. (2024). Timsort. Retrieved from https://en.wikipedia.org/wiki/Timsort

  2. Medium.com. AWDESH. Timsort: Fastest sorting algorithm for real world problems. Retrieved from https://awdesh.medium.com/timsort-fastest-sorting-algorithm-for-real-world-problems-1d194f36170e

  3. Peters, T. (2002). Timsort. Retrieved from https://svn.python.org/projects/python/trunk/Objects/listsort.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment