Insertion Sort
The key insight: insertion sort builds the sorted array one element at a time, from left to right. In each pass, we take the next element and shift larger elements in the sorted prefix one position to the right, creating a hole where the new element belongs. This is like sorting playing cards in your hand: pick up one card, compare it to the ones already held, and insert it in the correct position.
Details
Animated on: Sort nums = [5, 2, 4, 6, 1, 3] in ascending order using insertion sort.
Problem:Insertion SortEasy
Sorting
step 1 / 33
i = 1: pick up arr[i] = 2 as the key; the sorted prefix so far is just [5] to the left of index 1.
- i=1· arr[1] = 2
- key=1· arr[1] = 2
1FUNCTION insertionSort(arr):2 n = the length of arr3 FOR i from 1 to n-1:4 key = arr[i] (the value to insert)5 j = i - 16 WHILE j >= 0 and arr[j] > key:7 arr[j + 1] = arr[j] (shift the bigger value one step right)8 j = j - 19 arr[j + 1] = key (drop key into the hole)
← / → step · space play · Home restart
Every step in words
Show all 33 steps
- i = 1: pick up arr[i] = 2 as the key; the sorted prefix so far is just [5] to the left of index 1.
- j = i - 1 = 0: compare arr[j] = 5 with key = 2; since 5 > 2, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 5: shift element at index 0 right to index 1; the array now has a duplicate at index 1, and index 0 is vacated.
- j = j - 1 = -1: the while condition j >= 0 fails; we exit the loop and place key at arr[j + 1] = arr[0] = 2.
- arr[j + 1] = key = 2: place the saved key into the vacated position; the sorted prefix is now [2, 5].
- i = 2: pick up arr[i] = 4 as the key; the sorted prefix so far is [2, 5] to the left of index 2.
- j = i - 1 = 1: compare arr[j] = 5 with key = 4; since 5 > 4, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 5: shift element at index 1 right to index 2; the array now has 5 at index 2, and index 1 is vacated.
- j = j - 1 = 0: compare arr[j] = 2 with key = 4; since 2 <= 4, the while condition fails and we exit the loop.
- arr[j + 1] = key = 4: place the saved key into the vacated position at index 1; the sorted prefix is now [2, 4, 5].
- i = 3: pick up arr[i] = 6 as the key; the sorted prefix so far is [2, 4, 5] to the left of index 3.
- j = i - 1 = 2: compare arr[j] = 5 with key = 6; since 5 <= 6, the while condition fails immediately.
- arr[j + 1] = key = 6: place the saved key into the vacated position at index 2; the sorted prefix is now [2, 4, 5, 6].
- i = 4: pick up arr[i] = 1 as the key; the sorted prefix so far is [2, 4, 5, 6] to the left of index 4.
- j = i - 1 = 3: compare arr[j] = 6 with key = 1; since 6 > 1, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 6: shift element at index 3 right to index 4; the array now has 6 at index 4, and index 3 is vacated.
- j = j - 1 = 2: compare arr[j] = 5 with key = 1; since 5 > 1, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 5: shift element at index 2 right to index 3; the array now has 5 at index 3, and index 2 is vacated. (Continue shifting.)
- j = j - 1 = 1: compare arr[j] = 4 with key = 1; since 4 > 1, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 4: shift element at index 1 right to index 2; the array now has 4 at index 2, and index 1 is vacated.
- j = j - 1 = 0: compare arr[j] = 2 with key = 1; since 2 > 1, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 2: shift element at index 0 right to index 1; the array now has 2 at index 1, and index 0 is vacated.
- j = j - 1 = -1: the while condition j >= 0 fails; exit the loop and place key at arr[j + 1] = arr[0] = 1.
- arr[j + 1] = key = 1: place the saved key into the vacated position at index 0; the sorted prefix is now [1, 2, 4, 5, 6].
- i = 5: pick up arr[i] = 3 as the key; the sorted prefix so far is [1, 2, 4, 5, 6] to the left of index 5.
- j = i - 1 = 4: compare arr[j] = 6 with key = 3; since 6 > 3, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 6: shift element at index 4 right to index 5; the array now has 6 at index 5, and index 4 is vacated.
- j = j - 1 = 3: compare arr[j] = 5 with key = 3; since 5 > 3, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 5: shift element at index 3 right to index 4; the array now has 5 at index 4, and index 3 is vacated. (Continue shifting.)
- j = j - 1 = 2: compare arr[j] = 4 with key = 3; since 4 > 3, shift arr[j] rightward.
- arr[j + 1] = arr[j] = 4: shift element at index 2 right to index 3; the array now has 4 at index 3, and index 2 is vacated. (Now check the next condition.)
- j = j - 1 = 1: compare arr[j] = 2 with key = 3; since 2 <= 3, the while condition fails and we exit the loop.
- arr[j + 1] = key = 3: place the saved key into the vacated position at index 2; the sorted prefix now covers the entire array [1, 2, 3, 4, 5, 6]. Sorting complete.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.