Algorithms · Elementary Sorting Algorithms
Insertion Sort Algorithm
Overview
Insertion sort builds a sorted array the way a person sorts a hand of playing cards: a sorted "left hand" is maintained, and each new card is inserted into its correct position by comparing it from right to left.
It matters because it gives a simple, in-place sorting method whose behaviour on already-sorted and reverse-sorted input differs sharply ( versus ).
Key concepts
- Card-playing method: the left part of the array is the sorted hand; the right part is the unsorted deck. Pick one new card (the key) at a time and insert it into its correct place.
- Invariant: at any moment the already-processed part of the array is sorted. When the -th element is being examined, the first elements are already sorted.
- First element: a single element is already sorted, so nothing is done with it.
- Key: the element currently being inserted; its slot in the array is conceptually empty while shifting happens.
- Shifting: comparison proceeds from the rightmost element of the sorted part towards the left; every element greater than the key is shifted one place right, and the key is dropped into the gap where the first smaller element (or the array start) is found.
- Stopping condition: an element smaller than the key stops the shifting immediately (e.g. stopped the shifting for 7; stopped it for 1; stopped it for 3).
How it works
- Pick up the first element. A single element is already sorted, so nothing is done with it.
- Pick up the next element (the key); its slot is now conceptually empty.
- Compare the key with the elements of the sorted left part, starting from the rightmost element and moving left.
- Whenever a compared element is greater than the key, shift that element one position to the right, making room for the key.
- When an element smaller than the key is reached (or the beginning of the array is reached), place the key in the gap.
- The sorted left part has now grown by one element. Repeat from step 2 with the next element until all elements are inserted.
Restated as the loop: when picking up the element at position , examine positions down to 1; if the element there is greater than the key, move it right; if it is smaller, fix the key there.
Formulas
- Comparison/shift condition:
while j > 1 and A[j-1] > key: A[j] = A[j-1]; j = j - 1, thenA[j] = key- = the array, = the element currently being inserted, = current position being examined.
- Worst case for inserting into a sorted list of size : comparisons (the key is compared with every element of the sorted part).
- Worst case total: summing over gives comparisons, i.e. .
- Best case: the array is already sorted, so each key needs only 1 comparison and 0 shifts, giving total.
Worked examples
Array (1-indexed, positions 1 to 9): 9, 6, 5, 0, 8, 2, 7, 1, 3.
| Step | Key picked | Comparisons / shifts | Array after | Sorted part |
|---|---|---|---|---|
| 1 | 9 | none (single element) | 9, 6, 5, 0, 8, 2, 7, 1, 3 | [9] |
| 2 | 6 | 9 > 6, move 9 right; place 6 before it | 6, 9, 5, 0, 8, 2, 7, 1, 3 | [6, 9] |
| 3 | 5 | 9 > 5, move 9; 6 > 5, move 6; place 5 at front | 5, 6, 9, 0, 8, 2, 7, 1, 3 | [5, 6, 9] |
| 4 | 0 | move 9, move 6, move 5; place 0 at front | 0, 5, 6, 9, 8, 2, 7, 1, 3 | [0, 5, 6, 9] |
| 5 | 8 | 9 > 8, move 9; 6 < 8, place 8 after 6 | 0, 5, 6, 8, 9, 2, 7, 1, 3 | [0, 5, 6, 8, 9] |
| 6 | 2 | move 9, 8, 6, 5; 0 < 2, place 2 after 0 | 0, 2, 5, 6, 8, 9, 7, 1, 3 | [0, 2, 5, 6, 8, 9] |
| 7 | 7 | 9 > 7, move 9; 8 > 7, move 8; 6 < 7, place 7 after 6 | 0, 2, 5, 6, 7, 8, 9, 1, 3 | [0, 2, 5, 6, 7, 8, 9] |
| 8 | 1 | move 9, 8, 7, 6, 5, 2 each one position right; 0 < 1, place 1 after 0 | 0, 1, 2, 5, 6, 7, 8, 9, 3 | [0, 1, 2, 5, 6, 7, 8, 9] |
| 9 | 3 | move 9, 8, 7, 6, 5 each one position right; 2 < 3, place 3 after 2 | 0, 1, 2, 3, 5, 6, 7, 8, 9 | fully sorted |
Second illustration: starting from the array 9, 6, 5, 0, 1, 3, after the examination of the first four elements is over, the list on top is 0, 5, 6, 9 — by the time the algorithm reaches the next element, the entire prefix before it is sorted (0, 5, 6, 9).
Comparison
| Case | Input | Comparisons per key | Shifts | Total time |
|---|---|---|---|---|
| Best | already sorted | 1 | 0 | |
| Worst | key compared with every element of the sorted part | for a sorted part of size | up to | ( comparisons) |
Revision summary
- Invariant: when the -th element is examined, the first elements are already sorted.
- The first element needs no work — a single card is already a sorted hand.
- Comparisons run from the rightmost element of the sorted part towards the left; the key goes into the gap at the first smaller element (or at the array start).
- An element smaller than the key stops the shifting immediately.
- After the last element is inserted, the whole array is sorted: 0, 1, 2, 3, 5, 6, 7, 8, 9.
- Worst case is ; an already-sorted array is the best case at .