The O(n²) sorts shift values one step at a time. Merge and quick sort split the list instead — and n log n beats n² the moment the data gets big.
1 Divide and conquer
Split the list in half, sort each half, combine. Recursion does the splitting; the merge does the real work.
2 Merge sort
Always O(n log n) — even on the worst input — but it rents O(n) extra space to hold the merged output.
3 Quick sort
Partition around a pivot: smaller left, bigger right. Average O(n log n), worst O(n²) with terrible pivots.
4 One Lomuto pass
5 comparisons and 3 swaps drop pivot 19 exactly between the two zones — at index 2 of six values.
5 Python’s built-in
sorted() and list.sort() run Timsort — a stable merge sort hybrid that loves nearly-sorted data.
Divide and conquer
Divide and conquer1Split the list in half, sort each half, combine. Recursion does the splitting; the merge does the real work. Merge sort2Always O(n log n) — even on the worst input — but it rents O(n) extra space to hold the merged output. Quick sort3Partition around a pivot: smaller left, bigger right. Average O(n log n), worst O(n²) with terrible pivots. One Lomuto pass45 comparisons and 3 swaps drop pivot 19 exactly between the two zones — at index 2 of six values.
Python’s built-in5sorted() and list.sort() run Timsort — a stable merge sort hybrid that loves nearly-sorted data. 5 building blocks make up Divide and conquer
[29, 8, 44, 12, 35, 19] — the Lomuto partition packs the small values left, then drops 19 in the gap.
1 / 10nums = [29, 8, 44, 12, 35, 19]. Quick sort picks the LAST value — 19 — as the pivot. i marks the end of the “≤ pivot” zone: i = -1.
Split until single elements fall out, then merge on the way back up — recursion does the splitting for free.
process decision input / output start / end The final merge of the tree, zip by zip — then the Lomuto partition from the bars above.
moduleleft = [1, 3, 6, 9] — the left half after its own recursive sort.
Console
output appears as the program runs…
[6, 3, 9, 1, 7, 2, 8, 4] — split down to singles, then merge back up the tree level by level.
1 / 13merge_sort([6, 3, 9, 1, 7, 2, 8, 4]) — split first, merge later. The tree depth is log₂ 8 = 3.
One bare Lomuto sweep — track i, j and the swaps, then the pivot drop.
Your predicted output — one line per console line
Hint: pivot = a[-1] is 17. The sweep packs every value ≤ 17 into the front (count the swaps), then the pivot drops into the gap at i + 1.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?merge sortdrops into place
?O(n log n)drops into place
?pivotdrops into place
?Lomuto sweepdrops into place
?O(n) extradrops into place
?worst case O(n²)drops into place
?extend()drops into place
?Timsortdrops into place
01Merge sort: split to singles, merge upward — O(n log n) guaranteed, O(n) extra space.
02The merge step zips two sorted runs in one pass — at most n − 1 comparisons.
03Quick sort partitions around a pivot: Lomuto packs ≤ pivot left, then drops the pivot in the gap.
04Quick sort averages O(n log n) but degrades to O(n²) on awful pivots.
05sorted() and list.sort() run Timsort — a merge sort hybrid; recursion depth is only log₂ n.
06Merging needs both inputs sorted — that invariant is what makes the zip possible.