Given. Insertion sort on a list of $n$ elements, best case (already sorted) and worst case (reverse-sorted).
Find. (a) A description of the algorithm. (b) Best-case comparison count. (c) Worst-case comparison count. (d) Overall worst-case complexity.
(a) How insertion sort works. Starting from the second element, insertion sort repeatedly takes the next unsorted element and shifts it leftward past every already-sorted element that is greater than it, inserting it into its correct position within the growing sorted prefix — much like sorting a hand of playing cards one at a time.
(b) Already-sorted list. For each of the $n-1$ new elements (positions $2,\dots,n$), it is compared exactly once to its immediate left neighbour, found to be already $\ge$, and left in place (no shifting needed):
$$\boxed{n-1\ \text{comparisons.}}$$
(c) Reverse-sorted list. The $i$-th new element (for $i=2,\dots,n$) must be compared against and shifted past ALL $i-1$ elements already placed before it stops (every prior element is larger):
$$\sum_{i=2}^n(i-1) = \sum_{k=1}^{n-1}k = \frac{(n-1)n}{2} = \boxed{\frac{n(n-1)}{2}\ \text{comparisons.}}$$
(d) Worst-case complexity. $\frac{n(n-1)}{2}=\frac12n^2-\frac12n$ is dominated by its $n^2$ term:
$$\boxed{O(n^2).}$$
Final results — Question 10
Part
Result
(a)
Grows a sorted prefix by inserting each new element into place