week 6 - wednesday. what did we talk about last time? exam 1 post-mortem recursive running time
TRANSCRIPT
CS221Week 6 - Wednesday
Last time
What did we talk about last time? Exam 1 post-mortem Recursive running time
Questions?
Recursion
Assignment 3
Infix to Postfix Converter
Project 2
Wittman's Guide to Quick Debugging
1. a = b; a and b have to have the same type Exception: b is a subtype of a
2. == is a comparison operator that produces a boolean, = is a left-pointing arrow
3. if statements always have parentheses4. No semicolons after if or while headers (and rarely after for
headers)5. The following is always wrong:
x = y;x = z;
6. Know your String methods7. A non-void method has to return something8. The new keyword is always followed by a type and either
parentheses (possibly with arguments) or square brackets with integers inside
9. Local variables cannot be declared private, public, or protected
Know your syntax
By now, you should know your syntax inside and out
If you don't know how something in Java works, find out!
Syntax is your basic tool for everything Read about some syntax once? Doesn't help!
Write some code to see it in practice!
Syntax
Problem
Solving
Design
CS121
CS122
CS221
Exponentiation
We want to raise a number x to a power n, like so: xn
We allow x to be real, but n must be an integer greater than or equal to 0
Example: (4.5)13 = 310286355.9971923828125
Recursion for Exponentiation
Base case (n = 0): Result = 1
Recursive case (n > 0): Result = x ∙ x(n – 1)
Code for Exponentiation
public static double power( double x, int n )
{if( n == 0 )
return 1;else
return x * power( x, n – 1);}
Base Case
RecursiveCase
Running time for power
Each call reduces n by 1n total calls What's the running time?
Θ(n)
Can we do better?
We need to structure the recursion differently
Instead of reducing n by 1 each time, can we reduce it by a lot more?
It’s true that xn = x ∙ x(n – 1)
But, it is also true that xn = x(n/2) ∙ x(n/2)
New Recursion for Exponentiation
Assume that n is a power of 2 Base case (n = 1):
Result = x Recursive case (n > 1):
Result = (x(n/2))2
Code for Better Exponentiation
public static double power2( double x, int n )
{double temp;if( n == 1 )
return x;else{
temp = power2( x, n/2 );return temp * temp;
}}
Base Case
RecursiveCase
Running time for power2
Each call reduces n by half log2(n) total calls Just like binary search Can we expand the algorithm to
even and odd values of n?
Even Newer Recursion for Exponentiation
Base case (n = 1): Result = x
Recursive cases (n > 1): If n is even, result = (x(n/2))2
If n is odd, result = x ∙ (x((n – 1)/2))2
Code for Even Better Exponentiation
public static double power3( double x, int n ){double temp;if( n == 1 )
return x;else if( n % 2 == 0 ){
temp = power3( x, n/2 );return temp * temp;
}else{
temp = power3( x, (n – 1)/2 );return x * temp * temp;
}}
Base Case
RecursiveCases
Running time for power3
Each call reduces n by half (more or less) Θ(log2 n) total calls Does as well as power2() Better yet, we can use this solution to get
a logarithmic time answer for Fibonacci!
The nth term of the Fibonacci sequence is:
Where
Merge Sort
Merge Sort algorithm (recursive)
Beautiful divide and conquer Base case: List has size 1 Recursive case:
Divide your list in half Recursively merge sort each half Merge the two halves back together in
sorted order But how long does it take?
Master Theorem
Master Theorem
Has a great name… Allows us to determine the Big Theta
running time of many recursive functions that would otherwise be difficult to determine
Basic form that recursion must take
where
a is the number of recursive calls made b is how much the quantity of data is
divided by each recursive call f(n) is the non-recursive work done at
each step
Case 1
If for some constant , then
Case 2
If for some constant , then
Case 3
If for some constant , and if
for some constant and sufficiently large , then
Stupid Sort
Stupid Sort algorithm (recursive)
Base case: List has size less than 3 Recursive case:
Recursively sort the first 2/3 of the list Recursively sort the second 2/3 of the
list Recursively sort the first 2/3 of the list
again
Let's code up Stupid Sort!
But, how long does it take?
Stupid Sort
We need to know logb aa = 3b = 3/2 = 1.5 Because I’m a nice guy, I’ll tell you
that the log1.5 3 is about 2.7
Binary Search
We know that binary search takes O(log n) time
Can we use the Master Theorem to check that?
Quiz
Upcoming
Next time…
More on the Master theorem Symbol tables Read Chapter 3.1
Reminders
Finish Assignment 3 Due Friday, October 2, 2015
Keep working on Project 2 Due Friday, October 9, 2015