Introsort
Quicksort that watches its recursion depth. Too deep, it switches to heap sort. Small parts go to insertion sort. Fast on average, O(n log n) always.
SolvesSorting
- best
- O(n log n)
- average
- O(n log n)
- worst
- O(n log n)
- space
- O(log n)
Quicksort is fast on average, but bad pivots make it quadratic. Introsort keeps its speed and adds a safety net. It counts the recursion depth, and when a part is nested too deep, it hands that part to heap sort.
The last value is the median, so the first partition splits 3 and 4. Both parts are small, so quicksort and insertion sort finish the job and heap sort is never used.
working: quicksortdepth left = 3
1def insertion_sort(a, lo, hi):2 for i in range(lo + 1, hi + 1):3 key = a[i]4 j = i - 15 while j >= lo and a[j] > key:6 a[j + 1] = a[j]7 j = j - 18 a[j + 1] = key9 10 11def sift_down(a, root, size):12 while 2 * root + 1 < size:13 child = 2 * root + 114 if child + 1 < size:15 if a[child] < a[child + 1]:16 child = child + 117 if a[root] >= a[child]:18 break19 a[root], a[child] = a[child], a[root]20 root = child21 22 23def heap_sort(a):24 n = len(a)25 for start in range(n // 2 - 1, -1, -1):26 sift_down(a, start, n)27 for end in range(n - 1, 0, -1):28 a[0], a[end] = a[end], a[0]29 sift_down(a, 0, end)30 31 32def partition(a, lo, hi):33 pivot = a[hi]34 i = lo35 for j in range(lo, hi):36 if a[j] < pivot:37 a[i], a[j] = a[j], a[i]38 i = i + 139 a[i], a[hi] = a[hi], a[i]40 return i41 42 43def introsort_range(a, lo, hi, depth):44 if hi - lo + 1 <= 3:45 insertion_sort(a, lo, hi)46 return47 if depth == 0:48 part = a[lo:hi + 1]49 heap_sort(part)50 a[lo:hi + 1] = part51 return52 p = partition(a, lo, hi)53 introsort_range(a, lo, p - 1, depth - 1)54 introsort_range(a, p + 1, hi, depth - 1)55 56 57def introsort(a):58 depth = 059 size = len(a)60 while size > 1:61 size = size // 262 depth = depth + 163 introsort_range(a, 0, len(a) - 1, depth)64 return a8 values: too many for insertion sort.
- comparisons
- 0
- switches
- 0
How it works
Musser published the idea in 1997 as “introspective sort” [1]. Each call looks at the size of its part and the depth that is left:
- Small part. Up to 3 values here (real libraries use about 16): sort it with insertion sort.
- Depth limit reached. Sort the part with heap sort. It cannot degrade.
- Otherwise. Partition around the last value, then recurse into both sides with one less depth.
The depth limit starts at in this demo, so a short example can reach the switch. Real implementations use .
Complexity
| Case | Input | Time |
|---|---|---|
| Best | balanced partitions | |
| Average | random | |
| Worst | sorted (bad for last pivot) |
Quicksort alone would take on the worst input. Introsort stops the bad recursion after levels. The heap sort part costs . The stack stays at levels.
Implementations
- Standard: partition with the last value as pivot, as in the quicksort page. The switch to heap sort copies the part, sorts it and writes it back.
Pitfalls
- The sort is not stable. Both partitioning and heap sort reorder equal keys.
- The depth limit must count levels of one recursion path, not the total number of calls. Otherwise the fallback starts on inputs that are not bad at all.
Related
References
- D. R. Musser, “Introspective Sorting and Selection Algorithms,” Software: Practice and Experience, vol. 27, no. 8, pp. 983–993, 1997.
Further reading
- K. Mehlhorn and P. Sanders, Algorithms and Data Structures: The Basic Toolbox, Springer, 2008.