Step 3: Efficiency

An easy upper bound is obtained by bounding the two loops by n. This gives a running time of O(n2).

Is the worst-case running time $\Theta(n^2)$? That is, are there inputs for which running time is quadratic?

$\sum_{i=1}^{n-1} i = 1/2 (n-1)n = \Theta(n^2)$.

Is the running time $\Theta(n^2)$? That is, is it quadratic for all inputs? What is a best-case input for insertion sort?

$\sum_{i=1}^{n-1} 1 = n-1 = \Theta(n)$.


next up previous
Next: Measures of Sortedness Up: INSERTION SORT Previous: Step 2: Prove Correctness