Efficient Sorting Algorithms

Lesson 7 of 20

Watch a pivot sweep 19 into place, then follow merge sort’s recursion tree fold eight values into one sorted list.

The big picture

Divide and conquer

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.

Divide and conquer

Split the list in half, sort each half, combine. Recursion does the splitting; the merge does the real work.

Merge sort

Always O(n log n) — even on the worst input — but it rents O(n) extra space to hold the merged output.

Quick sort

Partition around a pivot: smaller left, bigger right. Average O(n log n), worst O(n²) with terrible pivots.

One Lomuto pass

5 comparisons and 3 swaps drop pivot 19 exactly between the two zones — at index 2 of six values.

Python’s built-in

sorted() and list.sort() run Timsort — a stable merge sort hybrid that loves nearly-sorted data.