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…
Category: Sorting
How smoothsort adapts a heap to sorted input
Short answer: Smoothsort keeps the unsorted prefix as a forest of implicit heaps whose sizes are Leonardo numbers, then peels maxima from the right. Sorted input costs Θ(n). Every input costs O(n log n). The sort is in place and unstable. Dijkstra designed it so a heap sort would…
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 selection sort bounds the number of writes
Short answer: Selection sort scans the unsorted suffix for its minimum and swaps that minimum into the next output slot. The comparison count is always Θ(n²). The write count is Θ(n) swaps, one per output slot. Use it when each write is expensive and the array is short enough…
How radix sort orders keys one digit at a time
Short answer: LSD radix sort stable-sorts fixed-width keys from the least significant digit to the most significant. Each digit is a counting sort on a radix-r alphabet. With d digits the time is Θ(d(n+r)). Sorting the least significant digit first is what makes a later digit pass keep the…
How quicksort recurses after a partition
Short answer: Quicksort partitions a slice around a pivot and sorts the two sides. With Lomuto’s partition the pivot ends at its final index and is excluded from both recursive calls. With Hoare’s partition the cut index is only a boundary between small keys and large keys, and the…
How patience sorting finds increasing subsequences
Short answer: Patience sorting deals each item onto the leftmost pile whose top is greater than or equal to the item. The number of piles equals the length of a longest increasing subsequence. With binary search over the pile tops, building the piles is O(n log n). A priority…
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 insertion sort charges one move per inversion
Short answer: Insertion sort grows a sorted prefix by sliding the next item left until it sits in order. Each slide removes one inversion, so the number of moves equals the inversion count. Sorted input is Θ(n). Reversed input is Θ(n²). The sort is stable and in place, and…
How heapsort sorts in place in n log n
Short answer: Heapsort turns the array into a binary max-heap, then repeatedly swaps the root with the last unsorted item and sifts the hole down. Building the heap is Θ(n). The n extractions are Θ(n log n) in the worst case. Extra memory is a few indices. The sort…
How external mergesort sorts beyond memory
Short answer: External mergesort sorts runs that fit in memory, writes them out, and merges them k at a time. With R runs and a k-way merge, the number of full passes over the file is ⌈log_k R⌉. Replacement selection with a heap of M records produces runs whose…
How cycle sort writes each item to its final index
Short answer: Cycle sort follows the cycle that a permutation forms and writes each misplaced item directly to the index its rank names. Items already in place are left untouched. The comparison count stays Θ(n²), because each rank is computed by scanning. The point of the algorithm is the…
How counting sort places integers from a histogram
Short answer: Counting sort histograms keys that lie in 0, 1, …, k−1, turns the histogram into prefix totals, and writes each record into the slot those totals name. The time and the extra memory are Θ(n+k). Writing from the end of the input and decrementing the total makes…
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…
How bubble sort finishes one suffix item per pass
Short answer: Bubble sort swaps every adjacent inversion and then shortens the unsorted prefix. After pass i the last i items are in their final places. A pass that swaps nothing means the prefix is sorted too, so the scan stops. The worst case remains Θ(n²) comparisons. It is…
How mergesort keeps a stable n log n bound
Short answer: Mergesort splits the array in half, sorts each half, and merges with two pointers. Ties take the left element, so equal keys keep their original order. Every level does Θ(n) work and there are Θ(log n) levels, which fixes the worst case at Θ(n log n) comparisons.…
