Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.

Similar presentations


Presentation on theme: "Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion."— Presentation transcript:

1 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion

2 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Announcements/Reminders l Project 7 due on Thursday April 14 l Exam 2 »Returned today »Average: ~ 72.5

3 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Recursive Methods Must Eventually Terminate A recursive method must have at least one base, or stopping, case. l A base case does not execute a recursive call »it stops the recursion l Each successive call to itself must be a "smaller version of itself" so that a base case is eventually reached »an argument must be made smaller each call so that eventually the base case executes and stops the recursion

4 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Remember : Key to Successful Recursion Recursion will not work correctly unless you follow some specific guidelines: The heart of the method definition can be an if-else statement or some other branching statement. l One or more of the branches should include a recursive invocation of the method. »Recursive invocations should use "smaller" arguments or solve "smaller" versions of the task. l One or more branches should include no recursive invocations. These are the stopping cases or base cases.

5 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Example: a Recursive Algorithm One way to search a phone book (which is an alphabetically ordered list) for a name is with the following recursive algorithm: Search : middle page = (first page + last page)/2 Open the phone book to middle page; If (name is on middle page) then done; //this is the base case else if (name is alphabetically before middle page) last page = middle page //redefine search area to front half Search //recursive call with reduced number of pages else //name must be after middle page first page = middle page //redefine search area to back half Search //recursive call with reduced number of pages

6 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Binary Search Code The find method of ArraySearcher implements a binary search algorithm l It returns the index of the entry if the target value is found or -1 if it is not found l Compare it to the pseudocode for the "name in the phone book" problem

7 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 7 57913323342545688 01234567890123456789 Indexes Contents target is 33 The array a looks like this: Binary Search Example mid = (0 + 9) / 2 (which is 4 ) 33 > a[mid] (that is, 33 > a[4] ) So, if 33 is in the array, then 33 is one of: 3342545688 56789 56789 Eliminated half of the remaining elements from consideration because array elements are sorted.

8 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 8 57913323342545688 01234567890123456789 Indexes Contents target is 33 The array a looks like this: Binary Search Example mid = (5 + 6) / 2 (which is 5 ) 33 == a[mid] So we found 33 at index 5 : 33 5 mid = (5 + 9) / 2 (which is 7 ) 33 < a[mid] (that is, 33 < a[7] ) So, if 33 is in the array, then 33 is one of: 3342 56 56 Eliminate half of the remaining elements

9 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Work Examples on Board... l Search for 55 (not in array!) 57913323342545688 01234567890123456789

10 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Factorial l The factorial of n is defined as: n! = 1 * 2 * 3 *... * (n-1) * n for n > 0 l E.g., 4! = 1 * 2 * 3 * 4 = 24 l What is a recursive definition for n! ? l Write a Java method to compute n!

11 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Factorial public static int factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); }

12 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Local Variables l When tracing recursive methods, it is important to remember that each call creates a copy of all local variables (this includes parameters and variables declared inside the recursive method). l Thus, local variables may have different values at each call!

13 public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2)

14 public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1)

15 public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=1 Return 1

16 public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1) As we saw in the previous slide, this returns 1 n is still 2 in this call, so this method returns 2 * 1

17 public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) As we saw in the previous slide, this returns 2 n is still 3 in this call, so this method returns 3 * 2 When this method returns, i will be 6

18 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Fibonacci Sequence l Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) l The sum of the n-th Fibonacci number is the sum of the previous two. l The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21,... l Write a recursive Java method for Fibonacci sequence (Notice that this problem is actually easier with recursion!)

19 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 19 Fibonacci Sequence public static int fibonacci (int n) { if (n==0) return 0; if (n==1) return 1; else return (fibonacci(n-1) + fibonacci(n-2)); }

20 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Merge Sort— A Recursive Sorting Method l Example of divide and conquer algorithm l Divides array in half and sorts halves recursively l Combines two sorted halves Merge Sort Algorithm to Sort the Array a If the array a has only one element, do nothing (stopping case). Otherwise, do the following (recursive case): Copy the first half of the elements in a to a smaller array named front. Copy the rest of the elements in the array a to another smaller array named tail. Sort the array front with a recursive call. Sort the array tail with a recursive call. Merge the elements in the arrays front and tail into the array a.

21 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Merge Sort: sort method public static void sort(int[] a) { if (a.length >= 2) { int halfLength = a.length / 2; int[] front = new int[halfLength]; int[] tail = new int[a.length – halfLength]; divide(a, front, tail); sort(front); sort(tail); merge(a, front, tail); } // else do nothing. } recursive calls make "smaller" problems by dividing array base case: a.length == 1 so a is sorted and no recursive call is necessary. do recursive case if true, base case if false

22 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Merge Sort: divide method private static void divide (int[] a, int[] front, int[] tail) { int i; for(i=0; i<front.length; i++) front[i] = a[i]; for(i=0; i<tail.length; i++) tail[i] = a[front.length+i]; }

23 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Merge Sort: merge method private static void merge (int[] a, int[] front, int[] tail) { int frontIndex=0, tailIndex=0, aIndex=0;

24 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Merge Sort: merge method (cont) while ( (frontIndex < front.length) && (tailIndex < tail.length) ) { if (front[frontIndex] < tail[tailIndex]) { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } else { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; }

25 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 25 Merge Sort: merge method (cont) while (frontIndex < front.length) { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } while (tailIndex < tail.length) { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; } } // end merge

26 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 26 Summary l If a method definition includes an invocation of the very method being defined, the invocation is called a recursive call. l Recursive calls are legal in Java and sometimes can make code easier to read. l To avoid infinite recursion, a recursive method definition should contain two kinds of cases: one or more recursive calls and one or more stopping cases that do not involve any recursive calls. l Recursion can be a handy way to write code that says "if there is a problem then start the whole process over again."


Download ppt "Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion."

Similar presentations


Ads by Google