Chapter 14 Review Exercise Solutions
R14.1 
Searching means to find an element in a collection of elements. Sorting means to arrange a collection of elements in a particular order.
R14.2 
Array length 0 or 1: i < a.length - 1 is not true when i equals 0, the for loop in the sort method never executes and the array is unchanged. That is ok.
Array length 2: The for loop in sort executes once, with i = 0. minimumPosition(0) is called. The for loop in that method executes from i going from 1 to less than 2, i.e. that loop also executes once. minPos is initially 0. If a[1] is less than a[0], then minPos is set to 1. When the method returns, if minPos is not 0 (i.e. a[1] < a[0]), then the two values are swapped, which causes the array to be sorted.
Array length 3: There are six possible variations of the array. Using 1, 2, 3 for the three elements, they are
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Here is a walk-through for the last scenario. The others are similar.
a[0] a[1] a[2]  i  minPos
3    2    1     0     0
3    2    1     0     2
1    2    3     0     2
1    2    3     1     1
1    2    3     1     2
R14.3 
n2+2n+1	O(n2)	n3-1000n2+109	O(n3)	
n10+9n9+20n8+145n7	O(n10)	n + log(n)	O(n)	
(n + 1)4	O(n4)	n2 + n log(n)	O(n2)	
(n2 + n)2	O(n4)	2n+n2	O(2n)	
n+0.001n3	O(n3)	(n3+2n)/(n2+0.75)	O(n)	
R14.4 
T(2000)/T(1000)	2004997/502497=3.99	f(2000)/f(1000)	4	
T(4000)/T(1000)	8009997/502497=15.94	f(4000)/f(1000)	16	
T(10000)/T(1000)	50024997/502497=99.55	f(10000)/f(1000)	100	
R14.5 
For a set of 2000 records, it will take 10 seconds. 
For a set of 10,000 records, it will take 50 seconds.
R14.6 
	O(n)	O(n2)	O(n3)	O(n log n)	O(2n)	
1000	5	5	5	5	5	
2000	10	20	40	11	can't compute	
3000	15	45	135	17	...	
10000	50	500	5000	67	...	
In the last column, the values get large too quickly. The following may be more helpful:
	O(2n)	
1000	5	
1001	10	
1002	20	
1003	40	
R14.7 
•	O(1)
•	O(log (n))
•	O(sqrt(n))
•	O(n)
•	O(n log (n))
•	O(n sqrt(n))
•	O(n2 log (n))
•	O(n3)
•	O(n log (n))
•	O(2n)
•	O(nn)
R14.8 
The array needs to be traversed once to find the minimum, so the time complexity is O(n), where n is the length of the array. Finding both the minimum and the maximum is no worse than 2O(n) = O(n)
R14.9 
The for loop is executed n times, where n is the length of the array. So it is an O(n) algorithm.
R14.10 
To look at each a[i] in turn requires n steps, where n is the length of the array. Each of these steps requires n - 1 element visits for the counting, and on average n / 2 steps for the element removal. Therefore, this is an O(n·(n - 1 + n/2)) = O(n2) algorithm.
R14.11 
Sorting takes O(n log n) time. After the array is sorted, we traverse it.
If we detect adjacent duplicates, we must move the remainder of the array down, which takes on average n/2 steps. So in the worst case this might be an O(n log n + n·n/2) = O(n2) algorithm.
However, you can easily do better. Sort the array (O(n log n)). Traverse the array, and whenever the next element is strictly larger, append it into a second array (O(n)). Then copy the second array back into the first (O(n)). The result is an O(n log n) algorithm. (You can even avoid the second array by moving the elements to the head the original array.)
R14.12 
The trouble is that we can't sort the array since that would disturb the original order. However, we can sort a copy to have a fast lookup for duplicates. Here is the algorithm.
Make a copy (O(n)) and sort it (O(n log n)). Make a companion boolean array to the sorted array and set all values in that array to false. Traverse the array and do the following n times:
For each element, use binary search in the copy (O(n)) and then look at the neighbors whether the element is a duplicate. If so, check if the corresponding value in the companion array is false, indicating that this element has never been used previously. If the element is not a duplicate or it has never been used, then append it to a second array, and mark the corresponding boolean as true. Since this needs to be done for each element, we have O(n log n) for this phase. The total time complexity is therefore O(n log n).
R14.13 
Selection sort has O(n2), even when the array is already sorted. On the other hand, insertion sort assumes the array is already sorted, and when it is, it iterates only once through the array. Thus, in the case that the array is already sorted, insertion sort has O(n).
R14.14 
No, it does not. The insertion sort algorithm finds the position where the element needs to be inserted at the same time that it moves the elements that need to be moved to make space for the new element. Thus, it does not actually take any extra iterations to find the position. Using a binary search will not improve the O(n2) order of the algorithm.