Sorted Part
In Hand
5
0
2
1
4
2
6
3
1
4

Card Sorting Strategy

Insertion Sort works like sorting cards in your hand. You pick up a new card and slide it left until it fits.

Pick Up 2

We pick the second element (2). This is our 'Active' card.

Compare with 5

Is 5 bigger than 2? Yes. So 5 must move to the right to make room.

Shift

Move 5 to the right. Now we have a 'hole' at index 0.

Insert

Hit the start of the array. Drop 2 into the hole.

Pick Up 4

Next card: 4. The Left side [2, 5] is sorted relative to itself.

Shift 5

5 > 4? Yes. Shift 5 right.

Compare 2

Check next neighbor: 2. Is 2 > 4? No. Stop!

Insert

Drop 4 into the spot after 2.

Pick Up 6

Next: 6. Compare with 5. 5 > 6? No. 6 is already in the right spot! (Best case scenario).

Pick Up 1

Last card: 1. This will need to slide all the way down.

Shift All

6 > 1 (Shift). 5 > 1 (Shift). 4 > 1 (Shift). 2 > 1 (Shift).

Insert at Start

Drop 1 at index 0.

The "Nearly Sorted" King

Notice how fast '6' was handled? If an array is nearly sorted, Insertion Sort is extremely fast (O(n)), beating even QuickSort and MergeSort.

Built for Stability.

Simple, stable, and amazing for small or partially sorted lists.

AlgoAnimator: Interactive Data Structures