Data Structures · Sorting

Insertion sort

Insertion sort works the way most people sort a hand of playing cards: pick up the next card and slide it left into its place among the cards already sorted. It is simple, it is fast on small or nearly sorted data, and it never reorders equal values.

The idea

The array is split into two parts by a boundary that moves from left to right:

Each pass does the same three things:

  1. Lift out the key: copy the first unsorted value, arr[i], into a variable called key. Its slot is now free.
  2. Shift larger values right: walk left through the sorted part. Every value bigger than the key moves one place right, into the free slot.
  3. Drop the key in as soon as you reach a value that is not bigger than it (or the start of the array).

After n − 1 passes every value has been inserted, so the whole array is sorted.

A worked example

Sorting {12, 11, 13, 5, 6}. Blue cells are the sorted part; the yellow cell is the key being inserted in that pass.

Try it

The array

Sorted part The key, lifted out Compared with the key (j) Unsorted

Variables

What is happening

main.cpp

    The array in main() changes to match what you sort.

    How much work does it do?

    Best case: already sorted. Each key is compared once with its left neighbor, which is not bigger, so nothing moves. That is n − 1 comparisons and no shifts: O(n).

    Worst case: reversed. Each key is smaller than everything in the sorted part, so it travels all the way to the front. Pass i makes i comparisons and i shifts, for a total of

    1 + 2 + … + (n − 1) = n(n − 1) / 2

    which is O(n²). Random data needs about half that: still O(n²).

    Insertion sort is adaptive: the closer the data is to sorted, the less work it does. A nearly sorted array costs barely more than n comparisons. It sorts in place with O(1) extra memory (just key, i and j).

    The same n, four kinds of input:

    Strengths and weaknesses

    Strengths
    • Simple to write and to reason about.
    • O(n) on sorted or nearly sorted data: it is adaptive.
    • Stable: equal values keep their original order. Try the Equal values preset.
    • In place, with O(1) extra memory.
    • Online: it can sort values as they arrive, one at a time.
    • Very fast for small arrays. Fast library sorts switch to insertion sort for short pieces.
    Weaknesses
    • O(n²) on random or reversed data.
    • Too slow for large arrays; merge sort and quicksort are O(n log n).
    • Can move many values: a small key near the end shifts the whole sorted part.

    Think first

    How many comparisons does insertion sort make on an already sorted array of 6 values?

    5: one per key, each against its left neighbor. No values move. Compare that with selection sort, which always makes 15.

    Why does the outer loop start at i = 1 instead of i = 0?

    The sorted part starts as just arr[0]. A single value is already sorted, so the first value to insert is arr[1].

    Why is insertion sort stable?

    A value is shifted only when it is strictly bigger than the key (arr[j] > key). An equal value stays put, so the key stops just to its right, and equal values never jump over each other. Changing > to >= would break this.

    Why do we copy arr[i] into key before shifting?

    The first shift writes arr[i - 1] into arr[i], overwriting the value we are inserting. Saving it in key first keeps it safe.