← chapter

Elementary sorts

Bubble, selection, insertion — all O(n²). Insertion is the one that matters.

Three ideas

Watch insertion sort

Blue prefix grows; orange element slides into place. Nearly-sorted data → barely any movement.

O(n²) vs O(n log n)

At n=2000, insertion is 500× slower than Timsort — and the gap only grows.

Insertion sort is adaptive

Takeaway

Insertion sort for small / nearly-sorted; O(n log n) for the rest. Learn to read a nested loop as a quadratic. Next: merge sort.