# (a there is no algorithm that sorts an array of n

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

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

View Full Document

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: (a) There is no algorithm that sorts an array of n elements and whose running time is O (7 n log n ), true or false? (b) Let A be an algorithm that, for each n > 0, takes less than 1000 steps for all inputs of size n except for one of these inputs, on which it takes exactly n steps. Then, the worst-case running time of A is Θ( n ), true or false? (c) In a Java array implementation of queues, the method enqueue has worst-case running time that is O (1), true or false? (d) There is no complete binary tree with 17 nodes, true or false? 2. (20 points) In this problem you NOT allowed to use any of the theorems about Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. Prove that if f ( n ) is Ω( n ) then 2 f ( n ) + n is O ( f ( n ). 3. (20pts) (This is an easy problem. You may possibly expect something harder like Problem 5 in Homework 3.) Consider the following recursive method: 2 static int Frodo(int n) { if (n==0) return 1; else { int y = Frodo(n-1); return n * y + n; } } Let F ( n ) denote the running time taken by the above method on the input n . (a) Analyze this code and formulate a recurrence relation for F ( n ). (b) For what input n does Frodo ( n ) simply terminate, without making a recursive call? Let n be that input. What is F ( n ) ? (c) Solve the above recurrence relation. 4. (20 points) static int foo(char a) { for (int j = 2; j < a.length; j = j*j ) a[j] = ’z’; return (a.length * a.length * a.length); } static void bar(char b) { for (int i = 1; i < foo(b); i=2*i) b[i]=’z’; } Analyze the worst-case running time of bar(b) as a function of n = b.length and give a Big-Oh bound. 5. (20 pts) A binary min-heap is storing 15 items in the array implementation starting with the root being at index 1. Write the sequence of array indices visited in a pre-order traversal of the tree. 6. (20 pts) Without altering the output, modify the following code to reduce the asymptotic upper bound on the worst-case running time, and provide an analysis of the improvement. You should include the original running time as well as the improved running time. public static int doSomething (int inArr, int foo) { int outArr = new int[inArr.length]; for (int i = 0; i < inArr.length; i++) { int bar = 0; for(int j = 0; j <= i; j++) { if (inArr[j] % foo == 0) { bar += inArr[j]; } } 3 outArr[i] = bar; } return outArr; } 4 Additional Problems 1. The order in which nodes are visited in the preorder traversal of a binary tree is exactly the reverse of the order in which nodes are visited in the postorder traversal, true or false? 2. (20 points) Give a function f ( n ) such that ( * ) f ( n ) X i =0 2 i is not O (2 n ) After you give f ( n ) prove ( * ) without using any of the theorems stated in the lecture notes, the textbook, or the lab notes. Your proof should rely only on the definition of Big-Oh and, of course, on the formula for the sum of a geometric progression that the review sheet told you to memorize.on the formula for the sum of a geometric progression that the review sheet told you to memorize....
View Full Document

{[ snackBarMessage ]}

### Page2 / 14

(a There is no algorithm that sorts an array of n elements...

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

View Full Document
Ask a homework question - tutors are online