Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.

Similar presentations


Presentation on theme: "Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion."— Presentation transcript:

1 Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion

2 Programming With Java ICS201 University Of Ha’il2 Recursive Methods o A recursive method is a method that calls itself directly or indirectly. o A recursive method has two major steps: 1.recursive step in which the method calls itself 2.base step which specifies a case with a known solution o The method should select one of two steps based on a criteria: Example: recursive step: fact(n) = n * fact(n-1) base step: fact(0) = 1

3 Programming With Java ICS201 University Of Ha’il3 Constructing Recursion o To construct a recursive algorithm you have to find out: 1.Recursive step 2.Base step o A selection structure is then used to determine which step to take.

4 Programming With Java ICS201 University Of Ha’il4 General Algorithm if (stopping condition) then solve simple problem (base) else use recursion to solve smaller problem combine solutions from smaller problem

5 Programming With Java ICS201 Recursive Methods 0! = 1 (By Definition!) n! = n x (n – 1) ! If n > 0 3! = 3 x 2! 2! = 2 x 1! 1! = 1 x 0! 0! = 1 (Base Case!) 1! = 1 x 0! = 1 x 1 = 1 2! = 2 x 1! = 2 x 1 = 2 3! = 3 x 2! = 3 x 2 = 6

6 Programming With Java ICS201 University Of Ha’il6 Recursive Methods recursive step: fact(n) = n * fact(n-1) base step: fact(0) = 1 fact(4) = 4 * fact(3) = 4 * (3 * fact(2)) = 4 * (3 * (2 * fact(1))) = 4 * (3 * (2 * (1 * fact(0)))) = 4 * (3 * (2 * (1 * 1))) = 4 * (3 * (2 * 1)) = 4 * (3 * 2) = 4 * 6 = 24

7 Programming With Java ICS201 7 Recursive Factorial Method

8 Programming With Java ICS201 8 Recursive Factorial Method public static int fact(int num) { if (num = = 0) return 1; else return num * fact(num – 1); }

9 Programming With Java ICS201 Fibonacci numbers n 01234567891011 Fib(n) 01123581321345589 University Of Ha’il9 public int fib (int n ) { if( n <= 1) { return n ; } else { return fib ( n - 1) + fib ( n - 2); } }

10 Programming With Java ICS201 University Of Ha’il10 Convert from decimal to binary This method converts an integer number to its binary equivalent. Base step: dec2bin(n) = n if n is 0 or 1 Recursive step: dec2bin(n) = dec2bin (n/2), (n mod 2) Algorithm dec2bin(n): If n < 2 Print n else dec2bin(n / 2) Print n mod 2

11 Programming With Java ICS201 University Of Ha’il11 Example (Recursion) class Method { public static void dec2bin( int n){ if ( n < 2 ) System.out.print( n ); else { dec2bin( n / 2 ); System.out.print( n % 2 ); } class Dec2Bin{ public static void main(String [] arg){ int i=10; dec2bin(i); } Output: 1010

12 Programming With Java ICS201 University Of Ha’il12 Example (iterative) public static void dec2bin(int n){ String binary =""; while ( n >= 1 ) { binary = n%2 + binary; n /= 2; } System.out.print(binary); }

13 Programming With Java ICS201 13 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 easier. Recursive solution is often slower.

14 Programming With Java ICS201 University Of Ha’il14 Exercise 1.Write a recursive method to find the greatest common divisor (GCD) of two integer n and m. 2.Write a recursive method to find X n given the double X and the integer n. 3.Consider a Boolean array b filled with Boolean values. Write a recursive method boolean allTrue() that returns true if all values are true and returns false otherwise.


Download ppt "Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion."

Similar presentations


Ads by Google