The same algorithm can take very different time on two inputs of the same size. Insertion sort is the standard example: on a sorted array it is fast, on a reversed array it is slow. This page counts its steps exactly and defines the three cases.
The three cases
The worst case is a guarantee: no input of size n will take longer. The best case is only a floor: no input of size n will take less. For every n:
Tbest(n)≤Tavg(n)≤Tworst(n).
Insertion sort and its cost table
Insertion sort works like sorting playing cards in your hand: take the next card (the key) and slide it left past every larger card until it is in place. In 1-based pseudocode (as in Cormen et al., §2.2), with n= length of A:
Insertion sort (pseudocode)
1 for j = 2 to n2 key = A[j]3 i = j - 14 while i > 0 and A[i] > key5 A[i + 1] = A[i]6 i = i - 17 A[i + 1] = key
Let tj be the number of times the line-4 test is evaluated for a given j. Each line has its own constant cost c1,…,c7:
Line
Cost
Times executed
1
c1
n (the test runs n−1 times true, once false)
2
c2
n−1
3
c3
n−1
4
c4
∑j=2ntj
5
c5
∑j=2n(tj−1)
6
c6
∑j=2n(tj−1)
7
c7
n−1
So the total running time is
T(n)=c1n+(c2+c3+c7)(n−1)+c4∑j=2ntj+(c5+c6)∑j=2n(tj−1).
Best case: the array is already sorted
For every j, the first test finds A[j−1]≤key, so tj=1 and lines 5 and 6 never run:
T(n)=c1n+(c2+c3+c4+c7)(n−1)=an+b
for constants a and b. The best case is Θ(n), linear.
Worst case: the array is in reverse order
Every key is smaller than all elements before it, so it is compared with all j−1 of them, plus the final test where i=0: tj=j. Using ∑j=1nj=2n(n+1):
∑j=2nj=2n(n+1)−1,∑j=2n(j−1)=2n(n−1).
Substituting gives T(n)=an2+bn+c for constants a>0, b, c. The worst case is Θ(n2), quadratic.
Average case
If all orderings are equally likely, each key is on average larger than about half of the elements before it, so tj≈j/2. The sum is about half of the worst case, still about n2/4, so the average case is also Θ(n2). “Average” does not mean “halfway between best and worst”.
Best case
Input already sorted tⱼ = 1 for every j T(n) = Θ(n)
Average case
Random order tⱼ ≈ j/2 T(n) = Θ(n²)
Worst case
Input in reverse order tⱼ = j T(n) = Θ(n²)
Insertion sort: the three cases
Key takeaways
Worst case = maximum over inputs of size n (a guarantee); best case = minimum; average case = expected value.
Insertion sort: tj=1 on sorted input (best, Θ(n)), tj=j on reversed input (worst, Θ(n2)).
Average case of insertion sort is Θ(n2) too.
Tbest≤Tavg≤Tworst for every n.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Show that the worst-case running time of insertion sort is quadratic.
Show worked answer
In the worst case (reverse order) each key is compared with every earlier element and the loop stops only when i=0, so tj=j. Then line 4 runs ∑j=2nj=2n(n+1)−1 times, and lines 5 and 6 run ∑j=2n(j−1)=2n(n−1) times each. Lines 1, 2, 3, 7 run n or n−1 times. So T(n)=c1n+(c2+c3+c7)(n−1)+c4(2n(n+1)−1)+(c5+c6)2n(n−1), which has the form an2+bn+c with a=2c4+c5+c6>0. Hence the worst-case time is Θ(n2).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-208/analysis-of-algorithms/best-worst-and-average-case-insertion-sort/ to read this page.