Skip to content
dsa/roadmap
3.3 ~40 min easy

Insertion Sort

Grow a sorted part on the left by inserting each new item into its correct place, like sorting cards in your hand. Fast on nearly sorted data.

Rule of this site: Don't just watch: implement it yourself, then solve 2–3 problems.

02Mental model

You’re dealt cards one at a time. Your hand is always sorted: each new card slides left past the bigger ones until it finds its spot. You never re-sort the whole hand. That’s insertion sort.

The idea

  • The part a[0..i-1] is always sorted.
  • Take key = a[i].
  • Walk left from i - 1, shifting every item bigger than key one place right.
  • When you hit an item ≤ key (or the start), drop key into the gap.

Why it’s adaptive

If the array is already sorted, every key is ≥ its left neighbour, so the while loop stops at once: n − 1 comparisons, O(n). If it’s reversed, every key travels all the way to the front: n(n−1)/2, O(n²). The less “out of order” the data, the faster it runs.

Shift, don’t swap

Each step of the while loop is one assignment (a[j+1] = a[j]), not a three-assignment swap. That’s why insertion sort usually beats bubble sort in practice, even though both are O(n²).

03Visualize

space play · ←→ step · r reset. The highlighted code line follows each step.

Loading visualizer…

Compare the "Sorted" preset (n − 1 comparisons) with "Reversed".

04C++ code

Compiled and run with g++ -std=c++17; the output below is real. Type it out yourself before you copy it.

insertion-sort.cpp22 lines · C++17
#include <iostream>
using namespace std;

void insertionSort(int a[], int n) {
    for (int i = 1; i < n; i++) {
        int key = a[i];
        int j = i - 1;
        while (j >= 0 && a[j] > key) {   // shift bigger items right
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = key;                  // drop key into the gap
    }
}

int main() {
    int a[] = {29, 10, 14, 37, 13, 5};
    int n = sizeof(a) / sizeof(a[0]);
    insertionSort(a, n);
    for (int i = 0; i < n; i++) cout << a[i] << " ";
    cout << "\n";
}
Expected output
5 10 13 14 29 37

05Complexity

Best case (already sorted)O(n): n − 1 comparisons, 0 shifts
Average caseO(n²)
Worst case (reversed)O(n²): n(n−1)/2 comparisons and shifts
Extra spaceO(1)
Stable?Yes
In-place?Yes

Insertion sort is adaptive (faster on nearly sorted input) and online (it can sort items as they arrive). Real libraries use it for small sub-arrays.

06Watch

Insertion sort algorithm

mycodeschool · 14:15

Why this one: Builds the "shift right, then drop the key" loop carefully and analyses the cases.

Insertion sort in 2 minutes

Michael Sambol · 2:19

Why this one: Two-minute animation to lock in the picture.

Original class slides (Python, by the course's lecture authors):Sorting Basics

07Exam lens

Likely questions

  1. Trace insertion sort on 12, 11, 13, 5, 6, showing the array after each insertion.
  2. What are the best and worst case inputs and complexities of insertion sort?
  3. Why does insertion sort perform well on nearly sorted data?
  4. Compare insertion sort with selection sort.

Common mistakes

  • Writing a[j] >= key in the while loop. That breaks stability.
  • Forgetting j >= 0 in the loop condition, which reads a[-1].
  • Putting a[j + 1] = key inside the while loop. It goes after the loop.
  • Starting the outer loop at i = 0. Item 0 alone is already "sorted", so start at 1.

08Quick check

  1. 1.Insertion sort on 2, 3, 4, 5, 1: how many shifts happen in total?
  2. 2.Best case of insertion sort?
  3. 3.What does the inner while loop do?
  4. 4.Which sort is best for an array that is already sorted except for 2 items?

09Practice

🟢 Trace by hand

Trace insertion sort on 12, 11, 13, 5, 6. Write the array after each value of i.

Show answer
  • i = 1 (key 11): 11 12 13 5 6
  • i = 2 (key 13): 11 12 13 5 6 (no shift)
  • i = 3 (key 5): 5 11 12 13 6 (3 shifts)
  • i = 4 (key 6): 5 6 11 12 13 (3 shifts)

🟡 Implement it

Write insertionSortDesc (descending order), then a version that sorts an array of string names alphabetically.

Hint

Only the comparison changes. string supports < and > out of the box.

🔴 Solve problems

  • Insertion Sort List mediumleetcode.com
    Hint

    Same idea on a linked list. Use a dummy head for the sorted list and insert each node after the right predecessor (revisit after Chapter 4).

  • Sort an Array mediumleetcode.com
    Hint

    Insertion sort will time out on the large tests, which shows why O(n²) matters.

  • Hint

    Print the array after every shift. A great tracing exercise.

10Mastery checklist

Tick these honestly. When every box is ticked, the topic is marked complete.

0/4 ticked

View as Markdown · Chapter cheat sheet

Keyboard shortcuts

⌘K /
Search topics, glossary, cheat sheets
space
Play / pause the visualizer
← →
Step back / forward
r
Reset the visualizer
?
Show this help