Download presentation
Presentation is loading. Please wait.
Published byAgnes Bailey Modified over 5 years ago
1
Java Programming: Chapter 9: Recursion Second Edition
From Problem Analysis to Program Design (Chapter 14), Second Edition
2
Chapter Objectives Learn about recursive definitions.
Explore the base case and the general case of a recursive definition. Learn about recursive algorithms. Learn about recursive methods. Learn about how to use recursive methods to implement recursive algorithms. Java Programming: From Problem Analysis to Program Design, Second Edition
3
Introduction // Program prints an array public class Print { // print array iteratively public static void printIterative( int[] array ) { for (int i=0; i< array.length; ++i ) System.out.print(array[i] + " "); } // end method printArray Java Programming: From Problem Analysis to Program Design, Second Edition
4
// recursively print array private static void printArrayHelper( int[] array, int startIndex ) { if ( startIndex == array.length ) return; // print elements in normal order System.out.print( array[ startIndex ] + " " ); printArrayHelper( array, startIndex + 1 ); } // end method printArrayHelper Java Programming: From Problem Analysis to Program Design, Second Edition
5
// recursively print array in reverse order private static void printArrayReverseHelper( int[] array, int startIndex ) { if ( startIndex == array.length ) return; // print elements in reverse order printArrayReverseHelper( array, startIndex + 1 ); System.out.print( array[ startIndex ] + " " ); } // end method printArrayHelper Java Programming: From Problem Analysis to Program Design, Second Edition
6
public static void main( String args[] ) { int array[] = { 8, 22, 88, 34, 84, 21, 94 }; System.out.print( "Array is: " ); printArrayHelper( array, 0 ); // print array System.out.print( "Array in reverse order is: " ); printArrayReverseHelper( array , 0 );// print array in reverse order } Java Programming: From Problem Analysis to Program Design, Second Edition
7
Recursive Definitions
Recursion: Process of solving a problem by reducing it to smaller versions of itself. Factorial of an integer: 0! = 1 n! = n * (n-1)! If n > 0 Java Programming: From Problem Analysis to Program Design, Second Edition
8
Recursive Definitions
Definition in which a problem is expressed in terms of a smaller version of itself. Has one or more base cases. Java Programming: From Problem Analysis to Program Design, Second Edition
9
Recursive Definitions
Recursive algorithm: Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself. Has one or more base cases. Implemented using recursive methods. Recursive method: Method that calls itself. Base case: Case in recursive definition in which the solution is obtained directly. Stops the recursion. Java Programming: From Problem Analysis to Program Design, Second Edition
10
Recursive Factorial Method
public static int fact (int num) { if (num == 0) return 1; else return num * fact(num-1); } Test to stop or continue. End case: recursion stops. Recursive case: recursion continues. Java Programming: From Problem Analysis to Program Design, Second Edition
11
Recursive Factorial Method
Java Programming: From Problem Analysis to Program Design, Second Edition
12
Tracing a Recursive Method
Has unlimited copies of itself. Every recursive call has its own: Code Set of parameters Set of local variables Java Programming: From Problem Analysis to Program Design, Second Edition
13
Tracing a Recursive Method
After completing a recursive call: Control goes back to the calling environment. Recursive call must execute completely before control goes back to previous call. Execution in previous call begins from point immediately following recursive call. Java Programming: From Problem Analysis to Program Design, Second Edition
14
Designing Recursive Methods
Understand problem requirements. Determine limiting conditions.( ex:the number of elements in a list) Identify base cases. Java Programming: From Problem Analysis to Program Design, Second Edition
15
Designing Recursive Methods
Provide direct solution to each base case. Identify general cases. Provide solutions to general cases in terms of smaller versions of general cases. Java Programming: From Problem Analysis to Program Design, Second Edition
16
Largest Value in Array public static int largest(int[] list,
int lowerIndex, int upperIndex) { int max; if (lowerIndex == upperIndex) return list[lowerIndex]; else max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return max; } Java Programming: From Problem Analysis to Program Design, Second Edition
17
Largest Value in Array Java Programming: From Problem Analysis to Program Design, Second Edition
18
Recursion or Iteration?
Two ways to solve particular problem: Iteration Recursion Iterative control structures use looping to repeat a set of statements. Tradeoffs between two options: Sometimes recursive solution is more natural and easier to understand. Recursive solution is often slower. Java Programming: From Problem Analysis to Program Design, Second Edition
19
When Not to Use Recursion
When recursive algorithms are designed carelessly, it can lead to very inefficient and unacceptable solutions. For example, consider the following: public int fibonacci( int N ) { if (N == 0 || N == 1) { return 1; } else { return fibonacci(N-1) + fibonacci(N-2); } Java Programming: From Problem Analysis to Program Design, Second Edition
20
Excessive Repetition Recursive Fibonacci ends up repeating the same computation numerous times. Java Programming: From Problem Analysis to Program Design, Second Edition
21
Nonrecursive Fibonacci
public int fibonacci( int N ) { int fibN, fibN1, fibN2, cnt; if (N == 0 || N == 1 ) { return 1; } else { fibN1 = fibN2 = 1; cnt = 2; while ( cnt <= N ) { fibN = fibN1 + fibN2; //get the next fib no. fibN1 = fibN2; fibN2 = fibN; cnt ++; } return fibN; Java Programming: From Problem Analysis to Program Design, Second Edition
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.