Solutions to other chapters:
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  
Java Methods Home Page Skylight Publishing



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.
6. (a) 6
(b) 7
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




Copyright © 2006 by Skylight Publishing