2.2 Analyzing algorithms
Express the function $n^3 / 1000 - 100n^2 - 100n + 3n$ in terms of $\Theta$-notation.
Consider sorting $n$ numbers stored in array $A$ by first finding the smallest element of $A$ and exchanging it with the element in $A$. Then find the second smallest element of $A$, and exchange it with $A$. Continue in this manner for the first $n - 1$ elements of $A$. Write pseudocode for this algorithm, which is known as selection sort. What loop invariant does this algorithm maintain? Why does it need to run for only the first $n - 1$ elements, rather than for all $n$ elements? Give the best-case and worst-case running times of selection sort in $\Theta$-notation.
1 2 3 4 5 6 7 8
SELECTION-SORT(A) n = A.length for j = 1 to n - 1 smallest = j for i = j + 1 to n if A[i] < A[smallest] smallest = i exchange A[j] with A[smallest]
The algorithm maintains the loop invariant that at the start of each iteration of the outer for loop, the subarray $A[1..j - 1]$ consists of the $j - 1$ smallest elements in the array $A[1..n]$, and this subarray is in sorted order. After the first $n - 1$ elements, the subarray $A[1..n]$ contains the smallest $n - 1$ elements, sorted, and therefore element $A[n]$ must be the largest element.
The running time of the algorithm is $\Theta(n^2)$ for all cases.
Consider linear search again (see Exercise 2.1-3). How many elements of the in- put sequence need to be checked on the average, assuming that the element being searched for is equally likely to be any element in the array? How about in the worst case? What are the average-case and worst-case running times of linear search in $\Theta$-notation? Justify your answers.
If the element is present in the sequence, half of the elements are likely to be checked before it is found in the average case. In the worst case, all of them will be checked. That is, $n / 2$ checks for the average case and $n$ for the worst case. Both of them are $\Theta(n)$.
How can we modify almost any algorithm to have a good best-case running time?
Modify the algorithm so it tests whether the input satisfies some special-case condition and, if it does, output a pre-computed answer. The best-case running time is generally not a good measure of an algorithm.