Insertion Sort - Algorithms in 60 Seconds
Channel: OpenAMind
Full video link:
https://www.youtube.com/watch?v=A6dwiFWO5bAAlgorithm • medium
Insertion sort keeps a growing sorted prefix and inserts each new value into its correct position.
It is especially strong for tiny datasets or nearly sorted input where each insertion shifts very little.
Many high-performance sort implementations still use insertion sort for small partitions.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Complexity Note 1 | Average/Worst O(n^2) |
| Best | O(n) |
| Space | O(1) |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Insertion Sort - Algorithms in 60 Seconds
Channel: OpenAMind
Full video link:
https://www.youtube.com/watch?v=A6dwiFWO5bAStep through Insertion Sort to see how the list changes over time.
Start with unsorted values.
Learn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Sorted prefix
What it is: Left side is maintained in sorted order.
Why it matters: Sorted prefix is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Current key
What it is: Value selected for insertion into sorted prefix.
Why it matters: Current key is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Shift loop
What it is: Moves larger values right to make room.
Why it matters: Shift loop is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Insert position
What it is: Final slot where the key is placed.
Why it matters: Insert position is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Outer pointer
What it is: Advances to next unsorted value.
Why it matters: Outer pointer is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Insertion position
What it is: Index where current value belongs in sorted prefix.
Why it matters: Index where current value belongs in sorted prefix. In Insertion Sort, this definition helps you reason about correctness and complexity when inputs scale.
Stable sort
What it is: Equal values preserve original relative order.
Why it matters: Equal values preserve original relative order. In Insertion Sort, this definition helps you reason about correctness and complexity when inputs scale.
Adaptive behavior
What it is: Runs faster when input is partially sorted.
Why it matters: Runs faster when input is partially sorted. In Insertion Sort, this definition helps you reason about correctness and complexity when inputs scale.
In-place sort
What it is: Sorts by mutating original array with O(1) extra space.
Why it matters: Sorts by mutating original array with O(1) extra space. In Insertion Sort, this definition helps you reason about correctness and complexity when inputs scale.
Shift
What it is: Move an element one slot right to open insertion space.
Why it matters: Move an element one slot right to open insertion space. In Insertion Sort, this definition helps you reason about correctness and complexity when inputs scale.
This walkthrough connects the core concepts of Insertion Sort into one end-to-end execution flow.
Step 1
Left side is maintained in sorted order.
Before
Array
After
Array
Transition
Why this step matters: Sorted prefix is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Step 2
Value selected for insertion into sorted prefix.
Before
Array
After
Array
Transition
Why this step matters: Current key is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Step 3
Moves larger values right to make room.
Before
Array
After
Array
Transition
Why this step matters: Shift loop is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
Step 4
Final slot where the key is placed.
Before
After
Transition
Why this step matters: Insert position is a required building block for understanding how Insertion Sort stays correct and performant on large inputs.
vs Bubble Sort
When to choose this: Choose insertion sort for fewer writes on nearly sorted input.
Tradeoff: Both are O(n^2) worst-case on random/reversed arrays.
vs Merge Sort
When to choose this: Choose insertion sort for small arrays where low overhead matters.
Tradeoff: Merge sort scales better for large input sizes.
vs Quick Sort
When to choose this: Choose insertion sort for tiny partitions in hybrid approaches.
Tradeoff: Quick sort dominates on larger random datasets.
Language runtime libraries
Standard library sort implementations often switch to insertion sort on very small partitions.
Takeaway: Simple algorithms can be optimal in specific ranges.
Spreadsheet software
Small row groups and incremental updates benefit from insertion-style behavior.
Takeaway: Nearly sorted data changes are often cheaper than full resorting.
Embedded systems
Firmware code paths with tiny arrays choose insertion sort for minimal implementation overhead.
Takeaway: Operational constraints can favor simplicity and predictability.
Take each value, shift larger values right, and insert into the gap.
Complexity: Average/Worst O(n^2), Best O(n), Space O(1)
Insertion sort with shifting
function insertionSort(values: number[]): number[] {
const sortedValues = [...values]
for (let insertionIndex = 1; insertionIndex < sortedValues.length; insertionIndex += 1) {
const currentValue = sortedValues[insertionIndex]
let compareIndex = insertionIndex - 1
while (compareIndex >= 0 && sortedValues[compareIndex] > currentValue) {
sortedValues[compareIndex + 1] = sortedValues[compareIndex]
compareIndex -= 1
}
sortedValues[compareIndex + 1] = currentValue
}
return sortedValues
}
Use these signals to decide if this data structure/algorithm is the right fit before implementation.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Relative Sort Array | Easy | O(n log n) |
| 2 | Sort an Array | Medium | O(n^2) insertion approach |
| 3 | Insertion Sort List | Medium | O(n^2) |
| 4 | Sort Colors | Medium | O(n) optimal, O(n^2) insertion naive |
| 5 | Largest Number | Medium | O(n log n) |
| 6 | Wiggle Sort II | Medium | O(n log n) |
| 7 | Sort List | Medium | O(n log n) target |
| 8 | Count of Smaller Numbers After Self | Hard | O(n log n) |
| 9 | Reverse Pairs | Hard | O(n log n) |
| 10 | Median of Two Sorted Arrays | Hard | O(log(min(m,n))) |