Sorting Algorithms – Basics
Lesson 6 of 20Watch 9 bubble to the end, the minimum snap into place and keys slide home — three O(n²) sorts, bar by bar.
Meet the basics
Three classic sorts share one idea — grow a sorted region one element at a time — but they disagree wildly on how much work each step costs.
Bubble: neighbour swaps
Each pass walks the array swapping out-of-order neighbours; the biggest value bubbles to the end. [6, 2, 9, 4, 7] is done in 3 passes.
The early exit
A swapped flag lets bubble sort quit after a pass with zero swaps — already-sorted input finishes in one clean O(n) sweep.
Selection: fewest swaps
Hunt the minimum, swap it into place. At most n - 1 swaps ever — but always about n²/2 comparisons, good input or bad.
Insertion: shift and place
Take the next key and shift bigger values right to slot it in. Nearly-sorted data is nearly free: best case O(n).
All in-place, all O(n²)
All three sort inside the original array with O(1) extra memory, and all hit quadratic time on hostile input.
Bubble in motion
Five bars, one bubble pass at a time — the biggest value drifts right until everything locks.
Five unsorted bars: [6, 2, 9, 4, 7]. Pass 1 scans j = 0..3, bubbling the biggest value to the right edge.
Anatomy of a pass
One bubble pass, stage by stage — the biggest value drifts right until it parks at the edge.
Walkthrough
Bubble sort with its early exit, then insertion sort shifting keys home — both print via Arrays.toString.
1import java.util.Arrays;23public class BubbleSort {4public static void main(String[] args) {5int[] a = {6, 2, 9, 4, 7};6int n = a.length;7for (int i = 0; i < n - 1; i++) {8boolean swapped = false;9for (int j = 0; j < n - 1 - i; j++) {10if (a[j] > a[j + 1]) {11int temp = a[j];12a[j] = a[j + 1];13a[j + 1] = temp;14swapped = true;15}16}17if (!swapped) break;18}19System.out.println(Arrays.toString(a));20}21}
a gets 5 unsorted values.
Selection in motion
Scan for the minimum, one swap per pass — the sorted prefix grows from the left.
Six bars: [5, 8, 2, 9, 1, 4]. Pass 1 scans all of them hunting the smallest value.
Predict the output
Count the swaps bubble sort makes — inversions are the whole story.
1public class Predict {2public static void main(String[] args) {3int[] a = {3, 7, 1, 5};4int swaps = 0;5for (int i = 0; i < 3; i++) {6for (int j = 0; j < 3 - i; j++) {7if (a[j] > a[j + 1]) {8int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;9swaps++;10}11}12}13System.out.println("swaps: " + swaps);14System.out.println(java.util.Arrays.toString(a));15}16}
Hint: Bubble swap count = the number of out-of-order pairs. Count the inversions in {3, 7, 1, 5} — the array ends fully sorted.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Bubble sort swaps neighbours; each pass locks the biggest remaining value at the right edge.
The swapped flag turns an already-sorted array into one clean O(n) pass.
Selection sort makes at most n - 1 swaps — best when writes are expensive — but always compares about n²/2 times.
Insertion sort is the card-player’s sort: shift bigger values right, drop the key in.
Nearly-sorted input → insertion sort shines; reversed input → all three hit O(n²).
All three are in-place with O(1) extra memory — and all three are the wrong tool beyond a few thousand elements.