Bubble, Selection and Insertion Sort
Trace and compare the three classic quadratic sorts, implement insertion sort, and learn what stability and in-place mean when choosing a sort.
Welcome back!
Step 1 of 9Learn
Why study slow sorts?
Sorting is everywhere: search results ranked by relevance, spreadsheets ordered by date, leaderboards, and countless algorithms that run faster on sorted data. Every language has a fast built-in sort, so why learn simple ones? Because they teach the core ideas, comparisons, swaps, invariants, stability, on small, understandable code, and because one of them, insertion sort, is genuinely used inside fast library sorts.
Bubble sort repeatedly walks through the array comparing adjacent pairs and swapping them if they're out of order. After the first pass, the largest element has bubbled to the end. After the second pass, the second largest is in place, and so on. Each pass needs one fewer comparison.
On [5, 1, 4, 2, 8], the first pass compares 5 and 1 (swap), 5 and 4 (swap), 5 and 2 (swap), and 5 and 8 (no swap), giving [1, 4, 2, 5, 8].
In the worst case, such as a reversed array, bubble sort does about n²/2 comparisons and as many swaps: O(n²). A common improvement is to stop early if a pass makes no swaps, which means the array is sorted. With that check, an already sorted array takes one pass: O(n) in the best case.
Bubble sort is simple but slow in practice, because it makes many swaps. It's mostly a teaching tool.
Step 2 of 9Practice · Choose one
What does the array [6, 2, 9, 1, 5] look like after one full pass of bubble sort, swapping adjacent pairs that are out of order from left to right?
Step 3 of 9Learn
Selection sort
Selection sort builds the sorted result from the left. On each pass, it scans the unsorted part to select the smallest element, then swaps it into the first unsorted position.
On [29, 10, 14, 37, 13]:
- Pass 1: smallest is 10; swap with 29 → [10, 29, 14, 37, 13].
- Pass 2: smallest of the rest is 13; swap with 29 → [10, 13, 14, 37, 29].
- Pass 3: smallest of the rest is 14; already in place.
- Pass 4: smallest of [37, 29] is 29; swap → [10, 13, 14, 29, 37].
The scans compare (n − 1) + (n − 2) + ... + 1 = n(n − 1)/2 pairs, no matter what the input looks like. Even a sorted array gets the full treatment, so selection sort is O(n²) in every case.
Its one strength is that it makes at most n − 1 swaps, the fewest of the simple sorts. That matters when writing to memory is much more expensive than reading, as with some flash memory.
Selection sort is not stable in its usual form: the long-distance swap can jump an element past an equal one. In pass 1 of [5a, 5b, 2], the 2 swaps with 5a, giving [2, 5b, 5a], and the two equal 5s have changed their relative order.
Step 4 of 9Practice · Work it out
How many comparisons does selection sort make to sort an array of 10 elements?
Step 5 of 9Learn
Insertion sort
Insertion sort works the way many people sort a hand of cards. The left part of the array is kept sorted. Take the next element, and slide it left past every larger element until it reaches its correct spot. Repeat until every element has been inserted.
On [8, 3, 5, 1]:
- Insert 3: it's smaller than 8, so 8 shifts right → [3, 8, 5, 1].
- Insert 5: shift 8 right, stop at 3 → [3, 5, 8, 1].
- Insert 1: shift 8, 5 and 3 right → [1, 3, 5, 8].
In the worst case, a reversed array, each element shifts past all earlier ones: about n²/2 operations, O(n²). But in the best case, an already sorted array, each element is compared once and stays put: O(n). More generally, insertion sort's work is proportional to how out of order the input is, so it's very fast on nearly sorted data, such as a sorted list with a few new items appended.
Insertion sort is stable, because an element only moves past strictly larger ones, and in place, using O(1) extra memory. It also has tiny constant factors. That's why high-performance sorts like Timsort, used in Python and in JavaScript engines, switch to insertion sort for small chunks of around 32 to 64 elements.
Step 6 of 9Practice · Code challenge
Implement insertionSort(arr) that sorts an array of numbers in ascending order in place using insertion sort, and returns the same array.
insertionSort([8, 3, 5, 1])insertionSort([1, 2, 3, 4])insertionSort([])insertionSort([3, -1, 3, 0, -1])insertionSort([10, 9, 8, 7, 6, 5])
Step 7 of 9Learn
Stability and in-place sorting
Two properties often matter as much as speed when choosing a sort.
A sort is stable if elements with equal keys keep their original relative order. Why care? Suppose a list of employees is already sorted by name, and you sort it by department. With a stable sort, employees within each department stay alphabetical. With an unstable sort, the names within a department can come out scrambled. Stability lets you sort by several keys in sequence: sort by the least important key first, then by the most important.
A sort is in place if it uses only O(1) extra memory beyond the input, rearranging elements within the array. This matters for large data sets or memory-constrained devices.
How the three simple sorts compare:
- Bubble sort: stable, in place, O(n²) worst, O(n) best with early exit, many swaps.
- Selection sort: not stable in the usual version, in place, O(n²) always, at most n − 1 swaps.
- Insertion sort: stable, in place, O(n²) worst, O(n) best, excellent on small or nearly sorted arrays.
In practice, insertion sort is the only one of the three with a real job, as the small-array fallback inside faster algorithms. For anything large, you want an O(n log n) sort, which is the subject of the next lesson.
Step 8 of 9Practice · Match the pairs
Match each simple sort to the property that best distinguishes it.
Tap an answer, then tap the place where it belongs (or drag it there).
- Insertion sort
- Selection sort
- Bubble sort
Step 9 of 9Practice · Choose one
A log file is almost perfectly sorted by timestamp, with only a few entries slightly out of place. Which simple sort will finish fastest?
Lesson complete!