Insertion Sort Illustrated- Visual Guide to Sorting Algorithms

What Is Insertion Sort, Anyway?

Insertion sort is one of those algorithms you probably already use without knowing it. Ever sorted playing cards in your hand? That's insertion sort.

The idea is dead simple: take each element and insert it into its correct position in a growing sorted portion of the array. One element at a time. That's it.

It's not flashy. It's not the fastest. But it's intuitive, works well on small or nearly-sorted data, and requires zero extra memory.

How Insertion Sort Works (Step by Step)

Let's sort this array using insertion sort:

[5, 3, 4, 1, 2]

Pass 1

Start with the second element (3). Compare it with everything before it.

[5, 3, 4, 1, 2] → 3 < 5, so swap them

[3, 5, 4, 1, 2]

Pass 2

Take element at index 2 (4). Compare with sorted portion [3, 5].

[3, 5, 4, 1, 2] → 4 < 5, swap

[3, 4, 5, 1, 2]

Pass 3

Take element at index 3 (1). Compare with sorted portion [3, 4, 5].

[3, 4, 5, 1, 2] → 1 < 5, swap

[3, 4, 1, 5, 2] → 1 < 4, swap

[3, 1, 4, 5, 2] → 1 < 3, swap

[1, 3, 4, 5, 2]

Pass 4

Take element at index 4 (2). Insert into sorted portion [1, 3, 4, 5].

After shuffling 2 left through the array:

[1, 3, 4, 5, 2] → [1, 3, 4, 2, 5] → [1, 3, 2, 4, 5] → [1, 2, 3, 4, 5]

Done. Sorted: [1, 2, 3, 4, 5]

When Insertion Sort Actually Makes Sense

Insertion sort shines in specific scenarios:

If you're building anything that handles live data streams or user input, insertion sort is worth considering.

When to Skip It

The honest truth: insertion sort is a learning tool and a niche production algorithm. It's not your default choice.

Insertion Sort vs. Other Sorting Algorithms

Algorithm Best Case Average Case Worst Case Space Stable
Insertion Sort O(n) O(n²) O(n²) O(1) Yes
Bubble Sort O(n) O(n²) O(n²) O(1) Yes
Selection Sort O(n²) O(n²) O(n²) O(1) No
Merge Sort O(n log n) O(n log n) O(n log n) O(n) Yes
Quicksort O(n log n) O(n log n) O(n²) O(log n) No

Notice insertion sort matches bubble sort on complexity. The difference is insertion sort does fewer comparisons on average because it stops once it finds the right spot. Bubble sort just keeps swapping blindly.

Getting Started: Implementation

Python

```python
def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr
```

JavaScript

```javascript
function insertionSort(arr) {
    for (let i = 1; i < arr.length; i++) {
        let key = arr[i];
        let j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
    return arr;
}
```

Both implementations are nearly identical. The core loop is:

  1. Store the current element
  2. Shift larger elements right
  3. Drop the stored element into the gap

Binary Insertion Sort: A Worthwhile Tweak

Standard insertion sort does a linear search to find where to insert. You can replace that with binary search — called binary insertion sort.

This cuts comparisons from O(n) to O(log n) per element. Total comparisons drop from O(n²) to O(n log n). Swaps stay at O(n²) worst case, but you're doing fewer operations overall.

For small arrays, the difference is marginal. For medium arrays (hundreds of elements), it's noticeable.

The Bottom Line

Insertion sort is the card-game algorithm. It's what humans naturally do when sorting physical objects. That intuitiveness makes it great for understanding sorting fundamentals.

Use it for:

Don't use it for production systems sorting large datasets. That's what heapsort and quicksort are for.