3.6.1 · D1Sorting & Searching

Foundations — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

1,652 words8 min readBack to topic

Before you can read a single line of the parent note, you must own every symbol it throws at you without warning. This page builds each one from nothing — plain words, then a picture, then why the topic needs it. Read top to bottom; each block uses only what came before.


1. An array and its index

So is the first box, the second, and the last box in a row of boxes is .

Figure — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

Why the topic needs it: every sentence like "shift A[i] left" or "swap index 0 with index 1" is meaningless until you can point at exactly one box. The index is the finger you point with.


2. The size n

Why the topic needs it: we want to describe speed as the array grows. Saying "it took 3 comparisons" is useless on its own; saying "it took about comparisons" tells you what happens when becomes a thousand or a million. Every cost formula in the parent note is a recipe with as the ingredient.


3. Comparison < and swap

Figure — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

These are the only two moves a comparison sort is allowed. It may look at two values and ask which is bigger, and it may move values between boxes. Nothing else.

Why the topic needs it: the parent note counts comparisons and moves as its measure of work. To count them you must first agree that these — and only these — are the atomic actions. That is exactly the comparison-sort model, and it is what makes the wall these three sorts cannot beat.


4. The inversion — the star of the whole topic

Figure — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

Read the picture: draw an arrow from every box to every box on its right. Colour the arrow red if the left value is bigger (out of order) and green if it is fine. The red arrows are the inversions.

Why the topic needs it: this single count is the reason insertion sort wins. Each time insertion sort slides an element one box left, it flips exactly one red arrow to green. Nearly-sorted data starts with few red arrows, so insertion sort barely works. See Inversions and Counting for the deeper theory, and Stability in Sorting for what happens to equal values (which are never inversions — is strict).


5. Adding up

Every cost formula in the parent note ends in the same sum, so we must own it. The symbol (Greek capital "sigma", for Sum) is shorthand for "add up a list":

Read it as: "let walk from up to , and add every you land on."

Figure — Bubble sort, selection sort, insertion sort — O(n²), when insertion sort wins

Why the topic needs it: selection sort's scan on pass makes fewer comparisons each time — , then , down to . Adding those up is this triangle. Same story for bubble's worst case and insertion's worst case. Knowing the sum is is knowing why all three are quadratic.


6. What "" actually says

Watch it happen to our triangle:

For the first piece is and the second is only — the term drowns everything. So we call it .

Why the topic needs it: "these three are all " and "insertion drops to when nearly sorted" are the parent note's two headline claims — both are statements in this language.


7. Best / worst / average case

Why the topic needs it: Big-O alone hides the punchline. Bubble and insertion share the worst case , yet insertion has a best case of that bubble-with-a-flag matches but selection can never reach. The whole "when insertion wins" argument lives entirely in the gap between best and worst. This adaptiveness is why real libraries — Timsort, Introsort — drop to insertion sort for tiny chunks before handing off to Merge Sort or Quicksort.


Prerequisite map

Array A and index i

Size n

Comparison and swap

Inversion = red arrow

Sum 1 to n-1 equals n times n-1 over 2

Big-O growth shape

When insertion sort wins

Best worst average case

Read it top-down: boxes and indices let you point; pointing lets you compare and swap; comparing defines the inversion; the triangle sum plus Big-O turn counts into growth shapes; and inversions + cases together explain the winner.


Equipment checklist

Cover the right side and answer each aloud. If any stumps you, re-read that section before opening the parent note.

What does mean, and what is the largest valid for an array of size ?
The value in box number , counting from 0; the largest valid index is .
What are the only two actions a comparison sort may perform?
Compare two values () and move/swap values between boxes.
Define an inversion in symbols.
A pair with — a left box holding a bigger value than a right box.
How many inversions does a reverse-sorted array of size have?
— every pair is out of order (the maximum possible).
What does equal, and what picture proves it?
; a triangle of dots that is exactly half of an rectangle.
In plain words, what does promise about doubling ?
The work grows roughly fourfold — it grows no faster than a constant times .
Why do best/worst/average matter when two sorts share the same worst-case ?
Because the best/average case is where they differ — insertion's best case is the whole reason it beats bubble and selection on tidy data.