Recursion1.1 - Last Updated 12-01-24 10:12 AM CSE 2011...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 6 - Friends & Strong Induction Recursive Algorithm: •Assume you have an algorithm that works. •Use it to write an algorithm that works. Use brute force to get into the smallest house. The “base case”
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 7 - Example 1 The factorial function: – n! = 1 · 2 · 3 · ··· · (n-1) · n Recursive definition: As a Java method: // recursive factorial function public static int recursiveFactorial( int n) { if ( n == 0 ) return 1 ; // base case else return n * recursiveFactorial ( n - 1 ); // recursive case } = = else n f n n n f ) 1 ( 0 if 1 ) (
Background image of page 2
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 8 - Tracing Recursion
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 9 - Linear Recursion recursiveFactorial is an example of linear recursion: only one recursive call is made per stack frame. // recursive factorial function public static int recursiveFactorial( int n) { if ( n == ) return 1 ; // base case else return n * recursiveFactorial ( n- 1 ); // recursive case } Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 10 - Recall: Design Pattern A template for a software solution that can be applied to a variety of situations. Main elements of solution are described in the abstract. Can be specialized to meet specific circumstances....
View Full Document

Page1 / 5

Recursion1.1 - Last Updated 12-01-24 10:12 AM CSE 2011...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online