5-Recursion - CMPT 225 Recursion CMPT 225 Objectives At the...

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

View Full Document Right Arrow Icon
CMPT 225 Recursion
Background image of page 1

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

View Full DocumentRight Arrow Icon
CMPT 225 Objectives At the end of this unit, we will be able to … Develop recursive algorithms and implement them Hand (box) trace recursive calls and compute their final result(s) Describe some advantages of tail recursion 2
Background image of page 2
CMPT 225 3 Iteration versus Recursion • In order to solve a problem, we may have to repeat (or iterate) some statements several times • This can be done using loops • Another way of repeating statements is to repeat the whole algorithm or operation itself • 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
CMPT 225 4 Recursive Algorithm/Operation • Definition: To solve a problem, a recursive algorithm/operation can achieve iteration (repeated statements) by calling itself either … • Directly: • Indirectly: recursive call operation recursive call recursive call operation 2 operation 1
Background image of page 4
CMPT 225 Base Case: Part of the problem (the algorithm is trying to solve) for which we know the answer Its purpose is to stops the algorithm from calling itself indefinitely Also called “terminating case” Recursive Case : Part of the problem for which we do not know the answer Therefore, in this step, the problem needs to be “modified” such that eventually, it does reach the point where it becomes “the part of the problem for which we know the answer” i.e., the base case This is where the algorithm/operation calls itself with a different (often smaller) version of the problem 5 Designing a Recursive Algorithm - 1
Background image of page 5

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

View Full DocumentRight Arrow Icon
CMPT 225 • Other components to consider: • name • parameters and their type • returned value type 6 Designing a Recursive Algorithm - 2
Background image of page 6
CMPT 225 Mystery Operation (Java) import java.io.*; class mysteryTestDriver { static public void main ( String [ ] args ) { System.out.println( "Calling mystery with n = 1 and max = 5." ); mystery( 1, 5 ); } static void mystery( int n, int max ) { for ( int i = n; i <= max; i++ )
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/29/2012 for the course CMPT 225 taught by Professor Annelavergne during the Summer '07 term at Simon Fraser.

Page1 / 24

5-Recursion - CMPT 225 Recursion CMPT 225 Objectives At the...

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

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