Sorting algorithms are fundamental procedures in computer science that rearrange elements in a collection according to a comparison operator. They form the backbone of data organization, enabling efficient searching, data analysis, and optimal algorithm performance across countless applicationsβfrom database indexing to machine learning pipelines. While the theoretical lower bound for comparison-based sorting is \Omega(n \log n), the real-world performance varies dramatically based on input characteristics, memory hierarchy, and implementation details β Python 3.11 replaced TimSort with the provably near-optimal PowerSort, DeepMind's AlphaDev discovered assembly-level improvements of up to 70% for small fixed-size arrays, and IPSβ΄o outperforms all known in-place and out-of-place parallel comparison sorts, proving the field far from settled.
What This Cheat Sheet Covers
This topic spans 18 focused tables and 101 indexed concepts. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
Table 1: Core Comparison-Based Sorts
The six foundational comparison-based algorithms every programmer should know. They differ sharply in stability, in-place memory use, and worst-case behavior β knowing when each one wins and when it collapses is the first layer of sorting mastery.
| Algorithm | Example | Description |
|---|---|---|
pivot = arr[mid]partition(arr, pivot)quicksort(left, right) | β’ Divide-and-conquer algorithm that selects a pivot and partitions array around it β’ average-case O(n \log n) but degrades to O(n^2) on poor pivot selectionβ’ fastest in practice for most random inputs; default basis for most production sort implementations. | |
divide(arr) β [left, right]merge(sorted_left, sorted_right) | β’ Stable divide-and-conquer sort with guaranteed O(n \log n) time in all casesβ’ requires O(n) auxiliary space for mergingβ’ excels with sequential access patterns, making it cache-friendly for large external datasets. | |
build_max_heap(arr)extract_max() β sorted | β’ In-place sort using a binary max-heap structure β’ consistent O(n \log n) time with O(1) space; build-heap phase is O(n)β’ not stable; slower than QuickSort in practice due to poor cache locality from non-sequential access. | |
for i in 1..n: shift arr[i] left while > prev | β’ Builds sorted array one element at a time by shifting β’ O(n^2) worst case but O(n) on nearly-sorted inputβ’ efficient for small arrays (n < 10β50) and used as base case in QuickSort, MergeSort, and TimSort. |