Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.

Similar presentations


Presentation on theme: "Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time."— Presentation transcript:

1 Week 6 - Wednesday

2  What did we talk about last time?  Exam 1 post-mortem  Recursive running time

3

4 Recursion

5 Infix to Postfix Converter

6 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 parentheses 4. 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 methods 7. A non- void method has to return something 8. 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

7  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 CS121CS122 CS221

8  We want to raise a number x to a power n, like so: x n  We allow x to be real, but n must be an integer greater than or equal to 0  Example: (4.5) 13 = 310286355.9971923828125

9  Base case (n = 0):  Result = 1  Recursive case (n > 0):  Result = x ∙ x (n – 1)

10 public static double power( double x, int n ) { if( n == 0 ) return 1; else return x * power( x, n – 1); } Base Case Recursive Case

11  Each call reduces n by 1  n total calls  What's the running time? Θ(n)Θ(n)

12  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 x n = x ∙ x (n – 1)  But, it is also true that x n = x (n/2) ∙ x (n/2)

13  Assume that n is a power of 2  Base case (n = 1):  Result = x  Recursive case (n > 1):  Result = (x (n/2) ) 2

14 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 Recursive Case

15  Each call reduces n by half  log 2 (n) total calls  Just like binary search  Can we expand the algorithm to even and odd values of n?

16  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

17 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 Recursive Cases

18

19

20  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?

21

22  Has a great name…  Allows us to determine the Big Theta running time of many recursive functions that would otherwise be difficult to determine

23  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

24

25

26

27

28  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

29

30

31  We need to know log b a  a = 3  b = 3/2 = 1.5  Because I’m a nice guy, I’ll tell you that the log 1.5 3 is about 2.7

32  We know that binary search takes O(log n) time  Can we use the Master Theorem to check that?

33

34

35  More on the Master theorem  Symbol tables  Read Chapter 3.1

36  Finish Assignment 3  Due Friday, October 2, 2015  Keep working on Project 2  Due Friday, October 9, 2015


Download ppt "Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time."

Similar presentations


Ads by Google