MCS-208 · Unit 1

Analysis of Algorithms

Section 6 of 8

Block 1 · Unit 1Analysis of Algorithms12 min · Section 6 of 8

1.6Best, Worst and Average Case: Insertion Sort

Covers IGNOU MCS-208 §1.3

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 nn will take longer. The best case is only a floor: no input of size nn will take less. For every nn: Tbest(n)≤Tavg(n)≤Tworst(n).T_{\text{best}}(n) \le T_{\text{avg}}(n) \le T_{\text{worst}}(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=n = length of AA:

Insertion sort (pseudocode)
1  for j = 2 to n
2      key = A[j]
3      i = j - 1
4      while i > 0 and A[i] > key
5          A[i + 1] = A[i]
6          i = i - 1
7      A[i + 1] = key

Let tjt_j be the number of times the line-4 test is evaluated for a given jj. Each line has its own constant cost c1,…,c7c_1, \dots, c_7:

LineCostTimes executed
1c1c_1nn (the test runs n−1n - 1 times true, once false)
2c2c_2n−1n - 1
3c3c_3n−1n - 1
4c4c_4∑j=2ntj\sum_{j=2}^{n} t_j
5c5c_5∑j=2n(tj−1)\sum_{j=2}^{n} (t_j - 1)
6c6c_6∑j=2n(tj−1)\sum_{j=2}^{n} (t_j - 1)
7c7c_7n−1n - 1

So the total running time is T(n)=c1n+(c2+c3+c7)(n−1)+c4∑j=2ntj+(c5+c6)∑j=2n(tj−1).T(n) = c_1 n + (c_2 + c_3 + c_7)(n - 1) + c_4 \sum_{j=2}^{n} t_j + (c_5 + c_6) \sum_{j=2}^{n} (t_j - 1).

Best case: the array is already sorted

For every jj, the first test finds A[j−1]≤keyA[j-1] \le key, so tj=1t_j = 1 and lines 5 and 6 never run: T(n)=c1n+(c2+c3+c4+c7)(n−1)=an+bT(n) = c_1 n + (c_2 + c_3 + c_4 + c_7)(n - 1) = an + b for constants aa and bb. The best case is Θ(n)\Theta(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−1j - 1 of them, plus the final test where i=0i = 0: tj=jt_j = j. Using ∑j=1nj=n(n+1)2\sum_{j=1}^{n} j = \frac{n(n+1)}{2}: ∑j=2nj=n(n+1)2−1,∑j=2n(j−1)=n(n−1)2.\sum_{j=2}^{n} j = \frac{n(n+1)}{2} - 1, \qquad \sum_{j=2}^{n} (j - 1) = \frac{n(n-1)}{2}. Substituting gives T(n)=an2+bn+cT(n) = an^2 + bn + c for constants a>0a > 0, bb, cc. The worst case is Θ(n2)\Theta(n^2), 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/2t_j \approx j/2. The sum is about half of the worst case, still about n2/4n^2/4, so the average case is also Θ(n2)\Theta(n^2). “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 nn (a guarantee); best case = minimum; average case = expected value.
  • Insertion sort: tj=1t_j = 1 on sorted input (best, Θ(n)\Theta(n)), tj=jt_j = j on reversed input (worst, Θ(n2)\Theta(n^2)).
  • Average case of insertion sort is Θ(n2)\Theta(n^2) too.
  • Tbest≤Tavg≤TworstT_{\text{best}} \le T_{\text{avg}} \le T_{\text{worst}} for every nn.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which input gives insertion sort its best case?

Show answer

Answer: A. An array already sorted in ascending order

On sorted input each key needs one comparison, so the time is linear. A one-element array is a smaller input, not a best-case arrangement of size nn.

Question 2 · multiple choice

For a reversed array of n=5n = 5 elements, how many times is the line-4 test evaluated in total?

Show answer

Answer: C. 14

∑j=25j=2+3+4+5=14=5⋅62−1\sum_{j=2}^{5} j = 2 + 3 + 4 + 5 = 14 = \frac{5 \cdot 6}{2} - 1. The value 10 is ∑(j−1)\sum (j - 1), the number of shifts, not tests.

Question 3 · multiple choice

How many line-4 tests does insertion sort make on an already sorted array of 10 elements?

Show answer

Answer: B. 9

One test for each j=2,…,10j = 2, \dots, 10: n−1=9n - 1 = 9. 54 is the worst case 10⋅112−1\frac{10 \cdot 11}{2} - 1.

Question 4 · multiple choice

What is the average-case time of insertion sort when all orderings are equally likely?

Show answer

Answer: D. Θ(n2)\Theta(n^2)

On average tj≈j/2t_j \approx j/2, which still sums to about n2/4n^2/4: quadratic. Θ(nlog⁡n)\Theta(n \log n) is the time of merge sort, not insertion sort.

Question 5 · multiple choice

Which statement about the worst case is correct?

Show answer

Answer: C. It is an upper limit on the time for every input of size nn.

The worst case is the maximum over all inputs of size nn, so no input of that size takes longer. It is about arrangement, not input size, and any notation (O, Ω, Θ) can describe it.

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=0i = 0, so tj=jt_j = j. Then line 4 runs ∑j=2nj=n(n+1)2−1\sum_{j=2}^{n} j = \frac{n(n+1)}{2} - 1 times, and lines 5 and 6 run ∑j=2n(j−1)=n(n−1)2\sum_{j=2}^{n} (j-1) = \frac{n(n-1)}{2} times each. Lines 1, 2, 3, 7 run nn or n−1n - 1 times. So T(n)=c1n+(c2+c3+c7)(n−1)+c4(n(n+1)2−1)+(c5+c6)n(n−1)2T(n) = c_1 n + (c_2 + c_3 + c_7)(n-1) + c_4\left(\frac{n(n+1)}{2} - 1\right) + (c_5 + c_6)\frac{n(n-1)}{2}, which has the form an2+bn+can^2 + bn + c with a=c4+c5+c62>0a = \frac{c_4 + c_5 + c_6}{2} > 0. Hence the worst-case time is Θ(n2)\Theta(n^2).

↑ ↓ move↵ openTab switch type