Text
Jul 25, 2026
Why Quicksort Fails When Data Doesn't Fit in RAM — External Merge Sort, Timsort, and Database Sort Optimisation
When data exceeds RAM, the bottleneck shifts from CPU comparisons to disk I/O — and quicksort's cache-friendly random access pattern becomes a liability while merge sort's sequential access pattern becomes an asset. Here's external merge sort's two-phase approach, why Timsort sorts nearly-sorted real-world data in close to O(n), how database query planners use B-tree index order to avoid explicit sort steps, and why `LC_ALL=C sort` produces different output than locale-aware sort on the same data.