Consider the following python code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Which of the following is the run time of
funky(A)
? Where
is the size of the input array
.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let f ( n ) be the run-time of the program. Then it is clear that for large enough n f ( n ) = f ( n / 2 ) + f ( n ) + n It is clear that f ( n ) ≥ n . Now we will show by induction that f ( n ) ≤ 6 n . The base case is easy. Assume that f ( n ) ≤ 6 n for all n ≤ m − 1 . Now we have f ( m ) = f ( m / 2 ) + f ( m ) + m = ( a ) ≤ 6 m / 2 + 6 m + m where ( a ) follows from our induction hypothesis. Assume m ≥ 9 , then we can bound m ≤ m / 3 . Hence from the above we get f ( m ) ≤ 3 m + 2 m + m = 6 m Which completes the inductive step. Hence, we have for all large enough n n ≤ f ( n ) ≤ 6 n Thus f ( n ) = Θ ( n ) .