Insertion Sort is one of the most intuitive sorting algorithms out there.
Imagine you’re sitting at a table playing a card game. The dealer hands you a pile of unsorted cards face down. You pick up the cards one by one to arrange them in your hand from smallest to largest.
Here is how your brain naturally handles it:
You pick up the first card. It’s a 7. Since it's the only card in your hand, it's already "sorted."
You pick up the second card. It’s a 3. You look at the 7, realize 3 is smaller, and insert it to the left of the 7.
You pick up a third card. It’s a 5. You compare it to the 7 (bigger, so you move past it) and then the 3 (smaller). You insert the 5 right between the 3 and the 7.
That is literally all Insertion Sort is! It builds a sorted list one item at a time by constantly "inserting" the current item into its correct position among the items already sorted.
How It Works (Step by Step)
Assume the very first element in the array is already sorted.
Move to the next element (let's call this the Key).
Compare the Key with the elements before it (to its left).
Shift all elements that are greater than the Key to the right to make room.
Insert the Key into its correct, empty slot.
Repeat for all remaining elements until the whole array is sorted.
The Walkthrough
Let's watch this happen with a real array of numbers: [5, 2, 4, 6, 1, 3].
Start: [5, 2, 4, 6, 1, 3] The first element (5) is our sorted zone.
Iteration 1: Key is 2. Compare 2 with 5. Since 5 is bigger, shift 5 to the right and insert 2.
Array becomes: [2, 5, 4, 6, 1, 3]
Iteration 2: Key is 4. Compare 4 with 5 (shift 5 right), then compare 4 with 2 (2 is smaller, so stop). Insert 4.
Array becomes: [2, 4, 5, 6, 1, 3]
Iteration 3: Key is 6. Compare 6 with 5. Since 6 is already bigger than 5, it's in the right spot! No shifting needed.
Array becomes: [2, 4, 5, 6, 1, 3]
Iteration 4: Key is 1. This is a tiny number, so it compares against 6, 5, 4, and 2, shifting all of them to the right, and drops into the very front.
Array becomes: [1, 2, 4, 5, 6, 3]
Iteration 5: Key is 3. Compare and shift 6, 5, and 4 to the right. Stop at 2. Insert 3.
Array becomes: [1, 2, 3, 4, 5, 6]
Example Code
Here is how we write this in clean, readable JavaScript. Paste this into your browser console or code editor to play around with it!
function insertionSort(arr) {
// Start from the second element (index 1) because index 0 is already "sorted"
for (let i = 1; i < arr.length; i++) {
let currentKey = arr[i];
let j = i - 1;
// Look backward through the sorted section.
// If an element is larger than our currentKey, shift it one position to the right.
while (j >= 0 && arr[j] > currentKey) {
arr[j + 1] = arr[j];
j--;
// Move left to check the next element
}
// Insert the currentKey into its correct, empty position
arr[j + 1] = currentKey;
}
return arr;
}
const numbers = [5, 2, 4, 6, 1, 3];
console.log("Sorted Array:", insertionSort(numbers)); // Output: [1, 2, 3, 4, 5, 6]🌟 Best Case: O(n)
Imagine you give this algorithm an array that is already sorted, like [1, 2, 3, 4, 5]. The algorithm will check each number once, realize it doesn't need to shift anything, and smoothly finish. It runs in linear time, which is incredibly fast!
🐌 Worst Case: O(n^2)
Imagine the array is completely backward, like [5, 4, 3, 2, 1]. For every single number, the algorithm has to compare and shift every single other number before it. This results in quadratic time. If your array grows 10x larger, the time it takes grows 100x larger!
🧳 Space Complexity: O(1)
Because Insertion Sort rearranges elements directly inside the original array without creating a copy or a new array, it uses virtually no extra memory. This is called sorting in-place.
Wrap up
If Insertion Sort can get terribly slow with large datasets (O(n^2)), why do we care about it?
Because it shines in specific real-world situations:
Small datasets: For lists with just a few items, Insertion Sort is actually faster than algorithms like Quick Sort because it has almost zero administrative overhead.
Nearly sorted data: If you are constantly adding a few new entries to a database that is already mostly sorted, Insertion Sort will clean it up in record time.









