homework3_001

homework3_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 3 - due...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 15-121 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 big-O 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.length-1; i++)if (a[i] == a[i+1])return true;return false;}a. What is the runtime complexity of this method using big-O notation in the worst case? Explain your answer. b. What is the runtime complexity of this method using big-O 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

Page1 / 4

homework3_001 - 15-121 FALL 2009 [CORTINA]HOMEWORK 3 - due...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online