hw3_solutions_final_withrubric

Answer here is the code from the lecture notes with

Info iconThis preview shows pages 2–5. 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 Document Right Arrow Icon

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

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: Answer Here is the code from the lecture notes with the modifications made in the problem: public static int twoThreeSearch(int a, int key) { return aux(a, 0, a.length, key); } private static int aux(int a, int bot, int top, int key) { if (bot >= top) return -1; int mid = (3*bot+2*top)/5 if (key < a[mid]) return aux(a, bot, mid, key); else if (key > a[mid]) return aux(a, mid+1, top, key); 12 of else return mid; } 2 (a) Show that twoThreeSearch terminates. To do so, exhibit a measure , i.e., a natural number that decreases by at least 1 with each recursive call. Answer The measure is top- bot . In one of the recursive calls it decreases to mid- bot = 3 bot +2 top 5- bot = 2( top- bot ) 5 in the other to top- mid- 1 = top- 3 bot +2 top 5- 1 = 3( top- bot ) 5- 1. Observe that both 2( top- bot ) 5 and 3( top- bot ) 5- 1 are at most top- bot- 1 when top- bot ≥ 1. When top- bot < 1 it’s the last recursive call anyway. (b) Analyze the code and give and give a Big-Oh characterization of the running time of twoThreeSearch . Explain your analysis. Answer This is like the analysis of binary in the lecture notes but the recurrence relation is a little different. To analyze the running time of aux , let n = top- bot and let T ( n ) be the running time of aux . In the worst case , the recursive call is always the one that then searches in the larger array portion, the one of size 3 n 5 . This sets up the following recurrence relation T ( n ) = T ( 3 n 5 ) + c Let ω = 5 3 . Just like in the lecture notes we assumed that n is a power of 2 we will assume here that n is a power of ω that is n = ω k We write T ( n ) = T ( n/ω ) + c T ( n/ω ) = T ( n/ω 2 ) + c ··· = ··· T ( ω ) = T (1) + c and when we add them up and simplify we get T ( n ) = T (1) + ck . Now T (1) is a constant (does not depend on n ). and k = log ω n = log n log ω . Therefore T ( n ) is O (log n ) and twoThreeSearch runs in O (log m ) where m is the size of its input array. This is the same asymptotic complexity as binary search. (a) - 7 points 4 points for a reasonable explanation 3 more points if mentioned ”measure” (b) - 13 points 4 points for correct recurrence relation 3 points for actually solving it 3 points for correct big-Oh bound 3 points for explanation Problem 3: 10 points In this problem you are NOT allowed to use the theorems about Big-Oh stated in the lecture notes. Your proof should follow just from the definition of Big-Oh. 3 Prove that for any f ( n ) (as usual mapping nonnegative reals to strictly positive reals) if f ( n ) is O ( n n ) then log f ( n ) is O ( n log n ). Answer From f ( n ) is O ( n n ) it follows that there exist N,c > 0 such that for all n ≥ N we have f ( n ) ≤ cn n ....
View Full Document

{[ snackBarMessage ]}

Page2 / 8

Answer Here is the code from the lecture notes with the...

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

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