Sorting one value into place

Pick the next value.
Slide it into place.

Insertion Sort grows a sorted region by taking one new value—the key—and moving it left past every larger neighbor.

Mini example[ 5, 2, 4 ]
01
524
pick key
02
254
insert
03
245
sorted

Key idea Before each pass the left region is sorted; inserting one key grows that region by one.

Before you start

What you will learn

A clear finish line for the lesson, plus the small amount of knowledge you need before beginning.

Learning objectives
  1. Explain why the left prefix remains sorted after every insertion.
  2. Trace the key, comparisons, and right shifts through a complete example.
  3. Implement Insertion Sort in JavaScript, Python, Java, or C++.
  4. Recognize why it is stable, adaptive, and useful for small or nearly sorted data.
01Understand02Visualize03Trace04Code05Complexity06Quiz07Challenge08Use cases

01 — The idea

How Insertion Sort thinks

Imagine sorting cards in your hand: where does the next card belong among the cards already ordered?

1
254

Pick up the key

Take the first value just outside the sorted region.

2
5→4

Shift larger values

Move each larger sorted value one position to the right.

3
245

Insert into the gap

Place the key in the opening and grow the sorted region.

02 — Try it yourself

Watch each key join the sorted region

Use your own numbers, then follow the key as larger neighbors shift right to open a gap.

3–10 values, each from 1 to 99
Pass — Paused
✓

Ready. The first value starts the sorted region.

Comparisons0
Shifts0
Sorted region1/7
Comparing Active key Sorted region
Dry-run trace

Turn each animation into a written decision

The latest eight Insertion Sort decisions
StepPassActionKey / neighborArray after stepShifts
0—Ready—[48, 22, 35, 11, 64, 28, 17]0

The highlighted row always matches the visualization above. Step forward to extend the trace.

03 — Read the code

From shifting values to working code

Compare how all four languages save the key, shift larger values, and write the key into its gap.

insertion-sort.mjs
01function insertionSort(numbers) {02  const array = [...numbers];03 04  for (let index = 1; index < array.length; index++) {05    const key = array[index];06    let position = index;07 08    while (position > 0 && array[position - 1] > key) {09      array[position] = array[position - 1];10      position--;11    }12 13    array[position] = key;14  }15 16  return array;17}

04 — Avoid the traps

Common mistakes

These bugs often produce an answer that looks almost right. Learn the symptom and the correction together.

01

Overwriting the key

Symptom: A value disappears when larger values shift right.

Correction: Save the key before shifting anything, then write it into the final gap.

02

Stopping one position too early

Symptom: A new smallest value never reaches index 0.

Correction: Continue while the position is above 0 and the left neighbor is larger.

03

Moving equal values

Symptom: Duplicate values reverse their original order.

Correction: Shift only values strictly greater than the key, not values equal to it.

04

Starting at index 0

Symptom: The code looks left of the array on its first pass.

Correction: Treat the first value as an already sorted region and begin with index 1.

05 — Check yourself

Check your understanding

Answer 4 questions, then reveal the reasoning. Your progress stays on this device—no account required.

0 of 4 answered
Q1What is guaranteed before each new pass?
Q2What is the best-case time complexity on already sorted input?
Q3Which shift condition preserves the order of equal values?
Q4Where should key 3 be inserted into the sorted region [1, 4, 6]?

Answer every question to check your work.

06 — Mini game

Insert the next card

Grow a sorted hand by choosing the correct gap for each new key.

Rule Select the gap after all smaller or equal cards and before the first larger card.
Insert key 40 picks
Next key4
25
↓

Choose the gap where 4 belongs in the sorted hand.

07 — Use it wisely

Where Insertion Sort fits

↗

Nearly sorted data

Few out-of-order values mean very few shifts, approaching linear time.

Great fit
+

Incremental input

New values can be inserted into an already sorted collection as they arrive.

Good fit
▦

Small datasets

Low overhead makes it useful inside hybrid sorts for small partitions.

Common fit
×

Large random datasets

Quadratic comparisons and shifts become expensive as input grows.

Avoid

Lesson progress

Finish this lesson with confidence

Use this private checklist to confirm what you learned. Progress stays only in this browser.

0 of 4 complete0%

Complete every checklist item to finish the lesson.