Recursion1.1

# Recursion1.1 - Last Updated 10:12 AM CSE 2011 Prof J Elder...

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

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”

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

View Full Document
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 ) (
Last Updated 12-01-24 10:12 AM CSE 2011 Prof. J. Elder - 8 - Tracing Recursion

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

View Full Document
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

## This note was uploaded on 02/14/2012 for the course CSE 2011Z taught by Professor Elder during the Fall '11 term at York University.

### Page1 / 5

Recursion1.1 - Last Updated 10:12 AM CSE 2011 Prof J Elder...

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

View Full Document
Ask a homework question - tutors are online