Java Software Structures, 4th Edition Exercise Solutions, Ch. 9
Chapter 9 Exercise Solutions
EX 9.1. Compare and contrast the linear search and binary search algorithms by searching
for the numbers 45 and 54 in the following list:
3 8 12 34 54 84 91 110
Searching for 45, which isn’t in the list, requires eight comparisons in a linear search to
EX 9.2. Using the list from Exercise 9.1, construct a table showing the number of
comparisons required to sort that list for each of the sort algoirthms (selection
sort, insertion sort, bubble sort, quick sort, and merge sort).
EX 9.3. Consider the same list from Exercise 9.1. What happens to the number of
comparisons for each of the sort algorithms if the list is already sorted.
The processing of selection and bubble sort, as written, is independent of the original
configuration of the items in the list, and therefore remain O(n2). But given that the data
is originally sorted, the inner loop of the insertion sort will only make one comparison
EX 9.4. Given the following list:
trace the execution for:
a. selection sort
Min 1, swap with 90: 1 8 7 56 123 235 9 90 653
Min 7, swap with 8: 1 7 8 56 123 235 9 90 653
Min 8, swap with 8: unchanged
Min 9, swap with 56: 1 7 8 9 123 235 56 90 653