quiz4-answer - EE 322C Data Structures Spring 2009 Dr....

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: EE 322C Data Structures Spring 2009 Dr. Ramesh Yerraballi Quiz 4 Points : 100 Due Date: ? NAME (First, Middle, Last):_______________________________________________ Instructions: • • • Please be specific in your answers. If any of the questions is ambiguous, make reasonable assumptions and document them along with your answers. Please type up or write down your final answers in this word document with prefix “Answer:” after each question. Before you begin, indicate your name above. 1. Give the worst case and best case complexities of the following code snippets using the BigOh notation (in terms of input size marked n). (4 + 4 + 6) = 14 points a) for( int i = 0 ; i < 10 ; i++ ) { .. } Answer: Best case : O(1) Worst case: O(1) b) int arr = initialize(); // returns an Integer array of size 'n' for(int i = 0 ; i < arr.length ; i ++) { if(arr[i] >= arr[arr.length]) break; } Answer: Best case : O(1) Worst case: O(n) c) int arr = initialize(); // returns an Integer array of size 'n' for(int i=0 ; i < arr.length ; i++) { boolean yes = true; for(int j=0 ; j < arr.length; j++) { if(arr[i] < arr[j]) yes = false; } if(yes) break; } Answer: Best case : O(n) Worst case : O(n ^ 2) 2. Give the worst case complexity of the following code snippet in terms of the Big Oh notation (4 + 4 + 6) = 14 points a) for ( int j = 0 ; j < n ; j += n/10) {.. // n is a multiple of 10} Answer: O(1) it always executes 10 times or less. b) for (int j = n; j > 0 ; j /= 10) { .. } Answer: O(log n) , in each iteration j reduces by a factor of 10 c) n>3 for( int i = 0; i < n ; i++) { //some code with no break or continue statements } for( int i = 0; i < n ; i++) { //some code with no break or continue statements } for( int i = 0; i < n ; i++) { //some code with no break or continue statements } Answer: O(n); Multiple loops only increase the constant factor. The algorithm is still linear time. 3. In a sentence, describe what the following code does to array 'arr' (15 points ) Bonus 5 points for giving the worst case time complexity. mystery(int arr,int i = 0, int j = length(L)-1) { if (arr[j] < arr[i]) { int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } if (j - i > 1){ int t = (j - i + 1)/3; mystery(arr, i , j-t); mystery(arr, i+t, j ); mystery(arr, i , j-t); } return arr; } int arr = initialize();//constructs an array of size n mystery(arr,0,arr.length-1); Answer: Stooge sort algorithm which runs in O(n ^ 2.7) 4. Show that log(n!) is O(n log n) (5 Points) Answer: log (n!) = log n + log (n-1) + log (n-2) + … + log 1 < log n + log n + log n … = n log n; 5. Answer the following two questions for a complete binary tree with n nodes: (4+4 Points) a) What is its height h in terms of n? Answer: (log2 (n+1)) – 1 or just O(log n) b) How many leaves does the tree have? (express this in terms of n) Answer: (n+1)/2 7. An algorithm takes 5 ms for input size 1000. How long will it take for input size of 3000. (Ignore lower order terms) if its running time is: linear, quadratic, n log n, cubic. (12 Points) Answer: Linear: T(n) = kn; 5 ms = k * 1000; k = 5 * 10-6; 15 ms. Quadratic: T(n) = k n2; Therefore, k = 5 * 10-9; 45 ms. n log n: T(n) = k n log n = 9965.78 k; Therefore, k = 5 * 10-7; 3.5 ms. Cubic: T(n) = kn3; k = 5 * 10-12; 135 ms. 8. An algorithm takes 1 ms for input size 1000. How large a problem can be solved in 3 minutes. (Ignore lower order terms) if its running time is: linear, quadratic, n log n, cubic. (12 Points) Answer: T(n2) = 3 minutes = 180, 000 ms; T(n1) = 1 ms; n1 = 1000; n2 = ? Linear: T(n) = k * n; We are given n1, T(n1) and T(n2) and we are asked n2 From 7, k = 10-6; T(n2) = kn2; substituting for k, 180, 000, 000 Quadratic: T(n) = k * n2. From 7, k = 10-9. Substituting for k, n2 = √(180*109) = 424264 n log n: T(n) = kn log n; From 7, k = 10-7; Substituting for k, n2 = 69,117,654 cubic: T(n) = k * n3. From 7, k = 10-12; Substituting for k, n2 = (180 * 10^12)^(1/3) = 56,462 9. Order the following in ascending order of growth rate (for example: c, b, a, d): (10 Points) a. f1(n) = 10n; b. f2(n) = n1/3; c. f3(n) = nn; d. f4(n) = (log2 n)(log2n) Answer: d, b, a, c; asymptotically b will overtake d; 10. Prove by Mathematical Induction the following statement : (10 points) For any number a , that ends with 4, a ^ n ends with 4 , if n is odd. Answer: ...
View Full Document

This note was uploaded on 09/26/2009 for the course EE 322C taught by Professor Nettles during the Spring '08 term at University of Texas.

Ask a homework question - tutors are online