This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 15121 FALL 2009 [CORTINA]HOMEWORK 3  due Wednesday, September 16 by 11:59PMElectronic handin for this assignment will be available Thurs after 5:00PM. PROBLEMS (10 pts)1. (3 pts) What is the runtime complexity of the following code fragments in bigO notation as functions of mand/or n? Explain each answer. (Your answers should represent the tightest/closest function for the code given.) a. int sum = 0;for (int i = 1; i <= n; i+=2)for (int j = 1; j <= n; j+=3)sum += (i+j);b. int sum = 0;for (int i = 1; i <= m; i*=2)for (int j = 1; j <= n; j++)sum += (i+j);c. int sum1 = 0;for (int i = 1; i <= n; i++)sum1 += i;int sum2 = 0;for (int j = 1; j <= n*n; j++)sum2 += j;2. (3 pts) Given n data values, an algorithm processes these n data values with a running time given by the equation T(n) = 7n2+ 6n + 8. a. Show that T(n) = O(n2). b. Is T(n) = O(n3)? Explain. c. Is T(n) = O(n)? Explain. 3. (1 pt) Consider the following Java method that returns true if an array of n integers has two adjacent values that are the same, false otherwise: public static boolean adjacentDuplicates(int a) {for (int i = 0; i < a.length1; i++)if (a[i] == a[i+1])return true;return false;}a. What is the runtime complexity of this method using bigO notation in the worst case? Explain your answer. b. What is the runtime complexity of this method using bigO notation in the best case? Explain your answer. 4. (1.5 pts) a. Suppose an algorithm processes ndata elements using exactly n2operations. If we double the number data elements, the number of operations needed will increase by a factor of what? b. Suppose an algorithm processes ndata elements using exactly n3operations. If we double the number data elements, the number of operations needed will increase by a factor of what? c. Suppose an algorithm processes ndata elements using exactly log2n operations. If we double the number data elements, the number of operations needed will increase by how much?...
View
Full
Document
This note was uploaded on 12/19/2009 for the course CS 121 taught by Professor Reidmiller during the Spring '09 term at Carnegie Mellon.
 Spring '09
 ReidMiller

Click to edit the document details