- Explain why the left prefix remains sorted after every insertion.
- Trace the key, comparisons, and right shifts through a complete example.
- Implement Insertion Sort in JavaScript, Python, Java, or C++.
- Recognize why it is stable, adaptive, and useful for small or nearly sorted data.
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.
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.
- Arrays and zero-based indexes
- Loops and value assignment
- Big O basics from the glossary
01 — The idea
How Insertion Sort thinks
Imagine sorting cards in your hand: where does the next card belong among the cards already ordered?
Pick up the key
Take the first value just outside the sorted region.
Shift larger values
Move each larger sorted value one position to the right.
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.
Turn each animation into a written decision
| Step | Pass | Action | Key / neighbor | Array after step | Shifts |
|---|---|---|---|---|---|
| 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.
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.
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.
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.
Moving equal values
Symptom: Duplicate values reverse their original order.
Correction: Shift only values strictly greater than the key, not values equal to it.
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.
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.
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 fitIncremental input
New values can be inserted into an already sorted collection as they arrive.
Good fitSmall datasets
Low overhead makes it useful inside hybrid sorts for small partitions.
Common fitLarge random datasets
Quadratic comparisons and shifts become expensive as input grows.
AvoidLesson progress
Finish this lesson with confidence
Use this private checklist to confirm what you learned. Progress stays only in this browser.
Complete every checklist item to finish the lesson.