Sub topic 14
Switching to Insertion Sort for Small Subarrays
This chapter assumes you know the in-place Lomuto quicksort from earlier chapters, how insertion sort works, and Big-O notation. Here you will add a size cutoff, so that quicksort hands small subarrays to insertion sort instead of recursing all the way down.
Why small subarrays are expensive
Quicksort's recursion keeps splitting until the subarrays have zero or one element. A large share of all calls therefore happen on tiny ranges. For a subarray of 5 elements, a function call, the pivot setup, and a partition loop cost more than the comparisons they save.
Insertion sort is O(n²) in general, but it has very low overhead per element. On a few elements it beats quicksort in practice. It also runs fast on data that is already close to sorted. Quicksort produces that kind of data: after a partition step, every element sits in the correct region.
Quicksort with a cutoff
The change is small. Before partitioning, check the size of the range. If it is at or below a threshold, sort it with insertion sort and return.
def lomuto_partition(a, lo, hi):
pivot = a[hi]
i = lo
for j in range(lo, hi):
if a[j] < pivot:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
return i
def insertion_sort(a, lo, hi):
for i in range(lo + 1, hi + 1):
key = a[i]
j = i - 1
while j >= lo and a[j] > key:
a[j + 1] = a[j]
j -= 1
a[j + 1] = key
CUTOFF = 10
def quicksort_cutoff(a, lo=0, hi=None):
if hi is None:
hi = len(a) - 1
if hi - lo + 1 <= CUTOFF:
insertion_sort(a, lo, hi)
return
p = lomuto_partition(a, lo, hi)
quicksort_cutoff(a, lo, p - 1)
quicksort_cutoff(a, p + 1, hi)
data = [29, 3, 17, 8, 41, 1, 12, 33, 5, 22, 9, 40, 2, 18]
quicksort_cutoff(data)
print(data) # [1, 2, 3, 5, 8, 9, 12, 17, 18, 22, 29, 33, 40, 41]Variant: one final pass
A second approach skips small ranges entirely during the recursion, leaving them unsorted, and then runs one insertion sort over the whole array at the end. The result is correct because every element is already inside its own small block, and the blocks are in order relative to each other. So no element moves more than CUTOFF positions. This variant makes fewer function calls. The per-range version keeps memory access local, which often helps on modern caches.
Choosing the threshold
The best cutoff depends on the machine and the cost of comparisons. Common values fall between about 8 and 20. GCC's libstdc++ uses 16. Measure on your own data. The curve is usually flat near the best value, so any number in that range gives most of the gain, typically 10 to 20 percent.
The cutoff does not change the asymptotic bounds. The average case is still O(n log n), and the worst case is still O(n²). It only lowers the constant factor.
Exercise
In the final-pass variant, why does the closing insertion sort over all n elements run in O(n · CUTOFF) time rather than O(n²)?
Show answer
When the recursion stops, every unsorted range has at most CUTOFF elements, and each range already holds exactly the values that belong in it. Each element shifts left only within its own range, so it moves fewer than CUTOFF positions. The total work is O(n · CUTOFF), which is linear for a fixed cutoff.
Exercise
Set CUTOFF = 1 in quicksort_cutoff. How does its behavior compare with plain quicksort?
Show answer
Ranges of size 0 or 1 go to insertion_sort, which does nothing on them. Every larger range gets partitioned. The function then behaves the same as plain quicksort, with no performance gain.