exam1-3sol

# exam1-3sol - (b StringBuilder r = new...

This preview shows pages 1–2. Sign up to view the full content.

1. (a) 2, 1, 53, 5, 23, ERROR (b) O(log n) - binary search O(n) - linear search O(1) - first element in array O(n) - liear search (c) double frequency; try { Scanner scan = new Scanner(System.in); System.out.print("Enter a FM radio frequency: "); frequency = Double.parseDouble(scan.nextLine()); if (frequency < 88.0 || frequency > 108.0) { frequency = 88.0; } } catch (NumberFormatException e) { frequency = 88.0; } 2. (a) 136, (2m+1)(n+1), O(mn) (b) 28, n(log2 m + 1), O(n log m) (c) O(log n), O(n) (d) 3 more operations 3. (a) Swaps the String at index with the previous String, if they both exist. O(1), O(n), O(n) (b) String temp = list.get(index); list.set(index, list.get(index-1)); list.set(index-1, temp); O(1) 4. (a) O(n^2)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (b) StringBuilder r = new StringBuilder(str.length); for (int index = str.length-1; index >=0; index--) { r.append(str.charAt(index)); } return r.toString(); O(n) (c) f(n) = n^3 T(n) = 2n^3 - 2n^2 +8n Find c>0 and n0>0 such that cf(n) >= T(n) for n > n0. c*n^3 >= 2n^3 - 2n^2 +8n c >= 2 - 2/n + 8/n^2 >0 when c = 3 for all n > n0 = 2 Thus T(n) = O(n^3) 5. int index = 0; while (index < numEntries && directory[index].getName().equals(name)) { index++; } if (index == numEntries) return null; // not found DirectoryEntry removed = directory[index]; System.arraycopy(directory, index+1, directory, index, numEntries-index-1); numEntries--; return removed;...
View Full Document

## This document was uploaded on 12/19/2009.

### Page1 / 2

exam1-3sol - (b StringBuilder r = new...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online