Insertion Sort Average Case – Comparisons, Shifts and Θ(n²)
At a glance
- 1. Count shifts as inversions
Each shift fixes exactly one inverted pair.
- 2. Average the inversions
Each pair is inverted with probability ½, giving n(n − 1)/4 shifts.
- 3. Add the comparisons
Each insertion costs about one comparison more than its shifts.
- 4. Conclude the growth
About n²/4 operations on average, which is Θ(n²) and half the worst case.
Insertion sort builds a sorted prefix one element at a time. Element is compared with the elements to its left, scanning right to left, and the larger ones are shifted one place to the right until the right spot is found:
def insertion_sort(a):
for i in range(1, len(a)):
key = a[i]
j = i - 1
while j >= 0 and a[j] > key: # one comparison per loop test with j >= 0
a[j + 1] = a[j] # one shift
j -= 1
a[j + 1] = key
“Average case” means the expected cost when the input is a uniformly random permutation of distinct keys.
Shifts: count the inversions
An inversion is a pair with but . Each shift fixes exactly one inversion and creates no new ones, so
Each of the pairs is inverted with probability in a random permutation. By linearity of expectation,
Comparisons: exact expectation
When the element at position (counting from 1) is inserted, the sorted prefix has elements, and the new key’s rank among the first is uniform. Say it moves past larger elements, where , each with probability .
- If , the loop makes successful comparisons plus one failing one: comparisons.
- If (the key is the new minimum), the loop stops because reaches , after comparisons.
So the expected number of comparisons for this insertion is
Summing over :
where . Equivalently, this is : one comparison per inversion, plus about one extra comparison per insertion.
Check, : the formula gives . By hand, the second element always costs 1 comparison, and the third costs on average, for a total of .
Best, average and worst case
| Case | Input | Comparisons | Shifts |
|---|---|---|---|
| Best | already sorted | ||
| Average | random permutation | ||
| Worst | reverse sorted |
So the average case is , with a constant about half that of the worst case.
Why insertion sort is still used
- On nearly sorted data its running time is , where is the number of inversions, which can be close to linear.
- It is stable, in place, and has very low overhead. Hybrid sorts such as Timsort and introsort switch to insertion sort for small subarrays (typically 16 to 64 elements).
- Binary insertion sort cuts comparisons to , but the shifts remain on average.
Common mistakes
- Saying the average case is "". That is the worst case. The average is half of that.
- Equating comparisons and shifts. They differ by about , which matters for small .
- Assuming duplicates don’t matter. The analysis above assumes distinct keys. With many equal keys there are fewer inversions.
References
- Knuth, D. E. (1998). The Art of Computer Programming, Vol. 3 – Sorting and Searching (2nd ed.), Section 5.2.1. Addison-Wesley.
- Cormen, T. H. et al. (2022). Introduction to Algorithms (4th ed.), Section 2.2. MIT Press.
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.