Final - CS 20 Final Exam Fall 2008 Analysis Recursion...

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

View Full Document Right Arrow Icon
CS 20 Final Exam Fall 2008 Analysis /15 Recursion /25 Binary Search Trees /25 BST Implementation /20 Heaps /20 Heap Implementation /20 Sorting /11 Hash Tables /30 General /70 Total /236
Background image of page 1

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

View Full DocumentRight Arrow Icon
Analysis: Derive the big-O notation of the following snippets of code. Begin with the summation and end with the big-O notation. If you are unsure of the summation, you will still get partial credit if you give an explanation for your big-O notation answer. If you end up with a summation that is more complicated than what you are responsible for, just leave it in terms of the summation and do not solve for big-O notation. a) for(i=0;i<n;i = i - 2) sum++; Summation from 0 to n/2 of 1-> n/2 -> O(n) b) for(i=1;i<n; i = i * 2) sum++; Summation from 0 to log2(n) of 1 -> log2(n) -> O(log2(n)) of O(logn) c)int recurse(int n) { if (n > 0) return( recurse(n-2) + n); else return 0; } Summation from 0 to n/2 of 1 -> n/2 -> O(n) d) int recurse(int n) { if (n > 0) return( recurse( n/3 ) * n); else return 0; } Summation of 0 to log3(n) of 1 -> log3(n) -> O(logn) or O(log3(n))
Background image of page 2
e) int recurse(int n) { for(i = 0; i < n; i++) sum++; if (n > 0) return (recurse (n/2) + sum); else return 0; } Summation of 0 to log2(n) of (summation of 0 to 2^n of 1) -> Summation of 0 to log2(n) of 2^n -> e) int recurse(int n) { for(i = 1; i < n; i *= 2) sum++; if (n > 0) return (recurse (n-1) + sum); else return 0; } Summation of 0 to n of (summation of 0 to log2(n) of 1) -> Summation of 0 to n of log2(n) -> Recursion: (25) Here is the code for a linear search using iteration. Rewrite it below using recursion. int findElement(Comparable item, Comparable[] array) { for(int i=0;i<array.length;i++) if (array[i].compareTo(item) == 0) return i; return -1; } (5) What is the base caes?
Background image of page 3

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

View Full DocumentRight Arrow Icon
(5) What is the recursive case? (10) Write the code (you may change the interface if necessary) length = length – 1; if (length < 0) return -1; else if (array[length].compareTo(item) == 0) return length; else return findElement(item, array, length); (5) What is the computational complexity of the above solution? Give the big-O notation and the reason. (25) Here is the code for a binary search using iteration. The assumption is that the array begins with sorted elements. Rewrite it below using recursion. int findElement(Comparable item, Comparable[] array, int length)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Final - CS 20 Final Exam Fall 2008 Analysis Recursion...

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

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