This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 COMP 202 –Introduction to Computing 1 COMP 202  Week 12 1 COMP 202 – Week 12 Recursion and Runtime Stack Learning outcomes – Defining procedures recursively – Writing recursive methods – Know about the memory organization of a Java program COMP 202 –Introduction to Computing 1 COMP 202  Week 12 2 Recursion COMP 202 –Introduction to Computing 1 COMP 202  Week 12 3 Recursive definition • Some concepts can be defined using the concept itself • Example: • Consider the following list of numbers: 24, 88, 40, 37 • Such a list can be defined as A LIST is a: number or a: number comma LIST 2 COMP 202 –Introduction to Computing 1 COMP 202  Week 12 4 Recursive definition • Sometimes, mathematical functions can be defined in terms of the same function • Example – n! = n * (n1)! 0! = 1 • How would you compute factorial, based on this definition? 5! = 5 * 4! = 5 * (4 * 3!) = 5 * (4 * (3 * 2!)) = 5 * (4 * (3 * 2 * (1!))) = 5 * (4 * (3 * 2 * (1 * (0!)))) • Where would you stop! • Yes, you have the definition of a special case. 0! = 1 5! = 5 * (4 * (3 * 2 * (1 * (1)))) • The special case is called base case COMP 202 –Introduction to Computing 1 COMP 202  Week 12 5 Factorial.java public class Factorial { public static int factorial (int n ){ /* Basis condition */ if (n == 0) return 1; else return n * facotrial ( n1); } public static int facotrial2 (int n){ /* calculate factorial in iterative manner */ int fact = 1; for (int i=1; i < n; i++){ fact = fact * i; } } public static void main (String args) { int X = 6; int factX = factorial (X); System.out.printf(“Factorial of %d is %d\n”, X, factX); factX = factorial2(X); System.out.printf(“Factorial of %d is %d\n”, X, factX); } } COMP 202 –Introduction to Computing 1 COMP 202  Week 12 6 Recursion • Sometimes a procedure to solve a problem can be defined by applying the same procedure on parts of the problem • Example: Reverse the String [0…(L1)] Reverse the String [1…(L1)] put the charAt(0) at the end 3 COMP 202 –Introduction to Computing 1 COMP 202  Week 12 7 StringReverse.java public class StringReverse { public static String reverse ( String s ){ if (s.length() <= 1) return s; else return reverse ( s . subString(1) )+s.charAt(0); } public static void main (String args) { Scanner kb = new Scanner(System.in); System.out.print(“Enter String to reverese: “); String str = kb.next(); System.out.println( reverse ( str )); } } Sample output: $java StringReverese Enter String to reverse: Harry yrraH $ COMP 202 –Introduction to Computing 1 COMP 202  Week 12 8 Recursion • Very often a recursive program is much simple than an iterative program • Two things about a recursive function or method – Recurrence relation – Basis condition • In the previous example: Recurrence relation: Reverese(string [0 to (L1)]) is equivalent to Reverse(substring[1 to L1]) appended with charAt(0) Basis condition: Reverse of a string with 1 letter is itself...
View
Full Document
 Fall '07
 VERBRUGGE
 Recursion, Subroutine, Harry yrraH

Click to edit the document details