Phase 2 · Searching & SortingModule 7~38 min read

Elementary Sorts

Bubble, selection, and insertion sort — simple, O(n²), and the perfect lens for learning to compare algorithms.

What you'll learn

Sorting is the most-studied problem in computing, and the three elementary sorts are where everyone starts. They're all O(n²), but comparing them teaches the vocabulary — comparisons, swaps, stability, in-place — you'll use to judge every algorithm after.

By the end you'll be able to:

  • Trace bubble, selection, and insertion sort
  • Explain what makes a sort stable and in-place
  • Know which elementary sort to prefer, and when

Bubble sort

Repeatedly walk the list, swapping any out-of-order neighbors. After each pass the largest remaining value "bubbles" to the end. Simple, but it does the most work of the three:

Bubble sort
Bubble sort
5
2
8
1
9
3
1/29Bubble sort: compare each pair of neighbors and swap them if they are in the wrong order.
Adjacent swaps push the largest value to the right each pass.

Selection sort

Find the smallest element and swap it to the front; repeat for the rest. It makes at most n swaps — the fewest of any comparison sort — but still O(n²) comparisons:

Selection sort
Selection sort
5
2
8
1
9
3
1/35Selection sort: repeatedly find the smallest value in the unsorted part and move it to the front.
Select the minimum of the unsorted part and move it into place.

Insertion sort

Grow a sorted region on the left, inserting each next value where it belongs — exactly how most people sort a hand of cards. It's the best elementary sort in practice: O(n) on nearly-sorted data and great for small arrays.

Insertion sort
Insertion sort
5
2
8
1
9
3
1/20Insertion sort: grow a sorted region on the left, inserting each next value where it belongs.
Each new element slides left into its correct spot in the sorted region.
Language
insertion_sort.py
def insertion_sort(a):
    for i in range(1, len(a)):
        key = a[i]
        j = i - 1
        while j >= 0 and a[j] > key:   # shift bigger items right
            a[j + 1] = a[j]
            j -= 1
        a[j + 1] = key                 # drop key into place
    return a

Stability & in-place

Two properties we'll ask of every sort:

  • Stable: equal elements keep their original relative order. This matters when sorting records by one field after another. Bubble and insertion are stable; selection is not.
  • In-place: uses only O(1) extra memory. All three elementary sorts are in-place.

Note

On tiny arrays (say fewer than ~16 elements) insertion sort beats fancy O(n log n) sorts because its constant factor is so low — which is why real library sorts fall back to it for small pieces.

Complexity

SortBestWorstStable?Swaps
BubbleO(n)O(n²)YesO(n²)
SelectionO(n²)O(n²)NoO(n)
InsertionO(n)O(n²)YesO(n²)

Recap & quick check

Key takeaways

  • Bubble, selection, and insertion sort are all O(n²) worst case and in-place.
  • Bubble and insertion are stable; selection is not.
  • Selection makes the fewest swaps (O(n)); insertion shines on nearly-sorted data.
  • Stable = equal items keep their order; in-place = O(1) extra memory.
  • Insertion sort is the go-to for small or almost-sorted arrays.

Quick check

1. Which elementary sort is best on nearly-sorted data?

2. What does it mean for a sort to be stable?

3. Which sort makes the fewest swaps?

4. What is the worst-case time of all three elementary sorts?

To go faster than O(n²) we need a better idea: divide and conquer. Next up: Module 8 — Merge Sort & Quick Sort.