Sorting Algorithms – Basics
Lesson 6 of 20Watch the biggest bar bubble home, a key slide into a sorted hand, and every pass shrink the unsorted rest.
Three ways to sort
Bubble, selection and insertion are the O(n²) trio — simple, in-place, and still the right tool for small or nearly-sorted data.
The O(n²) trio
All three use nested compare loops: roughly n²/2 comparisons for n items. Fine for 50 exam scores, painful for a million rows.
Bubble
Compare neighbours, swap when the left one is bigger. The largest value bubbles to the end each pass, and a swapped flag exits early once a pass changes nothing.
Selection
Scan the unsorted rest for the minimum, swap it to the front. Exactly n-1 swaps on every input — the fewest writes of the trio.
Insertion
Sorts like a hand of cards: lift the next key, shift bigger cards one slot right, drop the key into the gap. O(n) on nearly-sorted data.
Stability
Bubble and insertion keep equal keys in their original order; selection can reverse them. That matters when sorting by one column of many.
Still everywhere
std::sort hands tiny runs to insertion sort, and interviewers warm up with these three — master them before the O(n log n) big leagues.
Bubble sort live
Five bars, three real passes. Amber = the pair being compared, violet = a fresh swap, green = locked forever.
Bubble sort, pass 1: walk left to right comparing neighbours, swapping any out-of-order pair. The biggest value will bubble to the end.
Bubble’s loop
Two loops and one flag: the inner pass bubbles the biggest value home, the flag decides whether another pass is worth it.
Walkthrough
Selection sort hunts the minimum and swaps once per pass; insertion sort deals the array like a hand of cards.
1#include <iostream>2using namespace std;34int main() {5int a[] = {64, 25, 12, 22, 11};6int n = 5;78for (int i = 0; i < n - 1; i++) {9int minIdx = i;10for (int j = i + 1; j < n; j++) {11if (a[j] < a[minIdx]) minIdx = j;12}13int tmp = a[i];14a[i] = a[minIdx];15a[minIdx] = tmp;16}1718for (int i = 0; i < n; i++) cout << a[i] << " ";19cout << endl;20return 0;21}
a gets 5 unsorted scores — selection sort will grow a sorted prefix on the left.
Insertion in motion
A nearly-sorted hand: three inserts need zero shifts, only the 5 does real work.
Insertion sort grows a sorted prefix on the LEFT (green). Index 0 alone is trivially sorted; the right side is the unsorted rest.
Predict the output
A bubble-sort flight recorder: count every swap across every pass.
1#include <iostream>2using namespace std;34int main() {5int a[] = {9, 4, 7, 2};6int n = 4, swaps = 0;7for (int i = 0; i < n - 1; i++)8for (int j = 0; j < n - 1 - i; j++)9if (a[j] > a[j + 1]) {10int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;11swaps++;12}13cout << swaps << " swaps" << endl;14for (int i = 0; i < n; i++) cout << a[i] << " ";15return 0;16}
Hint: Pass i only scans pairs j < n-1-i. {9, 4, 7, 2} holds 5 out-of-order pairs (inversions) — and bubble sort performs exactly that many swaps.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Bubble: the biggest remaining value bubbles to the end each pass — the sorted suffix grows from the right.
A swapped flag makes bubble adaptive: zero swaps in a pass means stop — O(n) on sorted input.
Selection hunts the minimum and swaps once per pass: exactly n-1 swaps, the fewest writes of the trio.
Insertion grows a sorted prefix by shifting bigger cards right — nearly-sorted input makes it O(n).
All three are O(n²) worst case, but insertion usually wins in practice — real sorts use it for tiny runs.
Stability: bubble and insertion keep equal keys in order; selection does not.