Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

After this page, you should be able to:

Builds on: Read the algorithm pages, including merge sort and Timsort/Powersort.

Where does each algorithm create order?

StrategyProgress madeWork still required
Insertion sortExtend a sorted prefix by inserting one itemProcess the remaining suffix
Bubble sortPlace a largest remaining item at the right boundaryRepeat over the shorter prefix
Merge sortSort halves independentlyMerge their ordered outputs
QuicksortFix a pivot between two classified rangesSort each range

Merge sort’s split is cheap and its merge establishes the cross-half order. Quicksort spends linear work on partitioning before recursion; once its ranges are sorted, their order relative to the pivot is already known. Review the complete merge-sort trace and code rather than memorising only the shared nlog⁡nn\log n result.

Costs depend on the implementation

For constant-time key comparisons and list access, the implementations in this book have these costs for growing nn:

ImplementationBest timeAverage / expected time and assumptionWorst timeExtra spaceStable?Mutates input?
Shifting insertion sortΘ(n)Θ(n²), uniform random distinct-key permutationsΘ(n²)Θ(1)YesYes
Bubble sort with early stopΘ(n)Θ(n²), uniform random distinct-key permutationsΘ(n²)Θ(1)YesYes
Slicing merge sortΘ(n log n)Θ(n log n), for every input distributionΘ(n log n)Θ(n) peak including result and stackYes, left-first tiesNo
Last-pivot two-way quicksortΘ(n log n)Θ(n log n), uniform random distinct-key permutationsΘ(n²)Θ(log n) balanced; Θ(n) worst stackNoYes
Iterative heapsort in Topic 10Θ(n) on all-equal inputΘ(n log n), uniform random distinct-key permutationsΘ(n log n)Θ(1)NoYes
Timsort-derived built-in sortingΘ(n)Typically Θ(n log n) on random orderO(n log n)O(n) worst caseYes.sort(): yes; sorted(): no

Bounds describe n≥2 with constant-cost comparisons and ordinary list access. Θ(1) handles empty and singleton lists. “Extra space” includes recursion and any returned list; mutating input is a separate property. No timing benchmark is implied.

Quicksort’s best case assumes balanced partitions, not already sorted input. Its expected time needs an input-distribution or random-pivot assumption; it is not a synonym for best-case time. The merge-sort row describes the implementation without an already-ordered shortcut. The heapsort best case refers specifically to the early-stop sifts shown in Topic 10; a blanket Θ(n log n) best-case claim would overlook all-equal input. Powersort is the merge-policy explanation within the hybrid row, not an independently callable built-in.

Match the strategy to the requirement

For a small or nearly sorted sequence, insertion sort’s low overhead and Θ(n+I)\Theta(n+I) behaviour can be useful. Bubble sort provides a clear adjacent-swap model, but it can spend many comparisons even when few swaps are needed.

Merge sort offers predictable comparison growth and a natural stable merge, with additional storage for this list implementation. Quicksort offers constant-space partitioning and can be fast with suitable pivot and equal-key handling, while requiring care over worst-case time and recursion depth.

For ordinary Python application code, start with sorted or .sort: the implementation is optimised and the interface supports keys and stability. Choose a hand-written algorithm when its behaviour or constraints are the point of the exercise.

Before choosing, ask: must ties retain their order? May the original list change? How much extra memory is available? Is the input nearly ordered or full of repeated keys? Do we need a worst-case guarantee? Big-O alone does not answer these questions.

Check your understanding

Predict the answer before opening the explanation.

1. Which shown implementations are both stable and constant-extra-space?

Show explanation

Shifting insertion sort and early-stop bubble sort. Stable merge sort and the built-in hybrid may use linear storage.

2. Does sorted input give this last-pivot quicksort its best case?

Show explanation

No. It repeatedly selects an extreme pivot, producing quadratic time and linear recursive depth.

Next: The comparison lower bound.

For another comparison-based strategy, see heapsort and heap costs. Its shrinking active heap gives an in-place sorting approach with a different stability trade-off.