Solutions to other chapters:
Java Methods A & AB:
Object-Oriented Programming and Data Structures
Answers and Solutions to Exercises
Chapter 13
2. |
|
public int compareTo(Person other)
{
int diff = getLastName().compareTo(other.getLastName());
if (diff == 0)
diff = getFirstName().compareTo(other.getFirstName());
return diff;
} |
5. |
|
A few target values are much more likely
than the rest and these values are placed at the
beginning of the array. |
8. |
|
public static int search(int[] a, int m, int n, int target)
{
if (n <= m)
return -1;
int k = (m + n) / 2;
if (a[k] == target)
return k;
int pos = search(a, m, k-1, target);
if (pos >= 0)
return pos;
pos = search(a, k+1, n, target);
return pos;
}
|
11. |
(a) |
T -- the number of comparisons in Selection Sort is always the same. |
|
(b) |
F -- Insertion Sort takes O(n)
time if the array
is already sorted. |
14. |
|
0, 2, 3, 5, 7, 8, 1, 9, 4, 3 |
15. |
|
6, 9, 11, 10, 2, 22, 81, 74, 54 |
|