How timsort merges natural runs

Short answer: Timsort finds runs that are already ordered, extends short ones with insertion sort, and merges them under a stack invariant that keeps the merge cost at O(n log n). A strictly descending run is reversed so equal keys stay put, and the merge takes the left element…

How Shellsort chooses a gap sequence

Short answer: Shellsort insertion-sorts the array at a descending sequence of gaps and finishes with gap 1. An h-sorted array stays h-sorted when a later pass uses a smaller gap. The worst-case bound is a property of the gap sequence: Shell’s original halving sequence is Θ(n²), Hibbard’s 2^k−1 sequence…

How introsort caps quicksort with a heap

Short answer: Introsort runs quicksort until the recursion depth passes about 2 log₂ n, then finishes that slice with heapsort. Short slices drop into insertion sort. The worst case becomes O(n log n), the typical case keeps quicksort’s partition, and the sort stays in place and unstable. This is…

How bucket sort uses a uniform spread

Short answer: Bucket sort drops keys from a known numeric range into n buckets, sorts each bucket, and concatenates them. When keys are independent and uniform, the buckets stay small and the average time is Θ(n). One bucket that receives every key falls back to the inner sort, which…