Insertion sort
Insertion sort, step by step
Watch it sort any list, step by step, with its pseudocode and its Python.
Updated
Free, in your browser. Your lists never leave it.Privacy
The first value alone is in order.
What happens next?
The run stops five times and asks what comes next. Answer, and it plays on.
Pseudocode and implementation
Pseudocode
- The first value alone is a list in order.
- Take each next value, from left to right, and hold it aside: it is the one to insert.
- While there is a value on its left and that value is greater than the one held:
- move that greater value one place to the right.
- Put the held value in the gap that is left.
- Return the sorted list.
Python
def insertion_sort(a): for i in range(1, len(a)): key = a[i] j = i - 1 while j >= 0 and a[j] > key: a[j + 1] = a[j] j -= 1 a[j + 1] = key return aHow it works
Insertion sort keeps the left part of the list in order. It starts with the first value alone, then takes the next value, holds it aside, and moves every greater value on its left one place to the right. The held value goes into the gap that is left. It is how many people sort the cards in their hand.
Each shift fixes exactly one pair of values that were out of order, so the number of shifts is the number of such pairs. A sorted list needs no shifts and n − 1 comparisons; a list in reverse order needs about n²/2 of each.
It needs no extra memory, and it never moves a value past an equal one, so it is stable. On short or nearly sorted lists it is one of the fastest sorts, which is why faster sorts often switch to it for small pieces.
Complexity
| Best case | O(n) |
|---|---|
| Average case | O(n²) |
| Worst case | O(n²) |
| Extra space | O(1) |
| Stable | Yes |
| In place | Yes |
n is how many values the list has.
Questions
- When is insertion sort a good choice?
- For short lists and for lists that are nearly sorted, where it does little more than one comparison per value. For long random lists, merge sort or quicksort are much faster.
- How is it different from bubble sort?
- Both are O(n²) in the worst case and stable. Insertion sort grows a part in order from the left and slides each new value into it; bubble sort carries the greatest value to the end on each pass. Insertion sort usually makes fewer comparisons.
- What is the most common mistake?
- Swapping the value step by step instead of holding it aside. Keeping it in key and shifting the greater values writes each of them once, and the held value is written once, in its gap.
- How does "What happens next?" work?
- It runs insertion sort on a new random list and stops five times to ask what comes next: whether a value moves right, which pair is compared, or how the list looks after a value is inserted. Your answers stay in your browser.
More algorithms
Bubble sort
LiveWatch neighbours swap until the list is sorted, with pseudocode, Python and a quiz.Selection sort
LiveWatch each pass pick the smallest value and swap it to the front, with pseudocode, Python and a quiz.Merge sort
LiveWatch the list split into halves and merge back sorted, with pseudocode, Python and a quiz.Quicksort
LiveWatch each pivot split the list and land in its final place, with pseudocode, Python and a quiz.Binary search
LiveWatch the range halve until the value turns up, with pseudocode, Python and a quiz.AI text cleaner
LiveText from ChatGPT, Claude or Gemini without hidden characters, citation leftovers or broken spacing.