This preview shows pages 1–7. Sign up to view the full content.
Chapter 11
Chapter 11
●
Basics of Recursion
●
Programming with Recursion
Recursion
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Chapter 11
Overview
Recursion
: a definition in terms of itself.
Recursion in algorithms:
●
Recursion is a natural approach to some problems
»
it sounds circular, but in practice it is not
●
An
algorithm
is a stepbystep set of rules to solve a problem
»
it must eventually terminate with a solution
●
A
recursive algorithm
uses itself to solve one or more subcases
Recursion in Java:
●
Recursive methods implement recursive algorithms
●
A
recursive method
in one whose definition includes a call to itself
»
a method definition with an invocation of the very method used to define it
Chapter 11
Recursive Methods
Must Eventually Terminate
A recursive method must have
at least one base, or stopping, case.
●
A base case does not execute a recursive call
»
it stops the recursion
●
Each successive call to itself must be a "smaller version of
itself" so that a base case is eventually reached
»
an argument must be made smaller each call so that
eventually the base case executes and stops the recursion
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Chapter 11
Example: a Recursive Algorithm
One way to search a phone book (which is an alphabetically
ordered list) for a name is with the following recursive algorithm:
Search:
middle page = (first page + last page)/2
Open the phone book to middle page;
If (name is on middle page)
then done;
//this is the
base case
else if (name is alphabetically before middle page)
last page = middle page
//redefine search area to front half
Search
//recursive call with
reduced number of pages
else //name must be after middle page
first page = middle page
//redefine search area to back half
Search
//recursive call with
reduced number of pages
Chapter 11
Example: A Recursive Method
●
RecursionDemo
is a class to process an integer and print out its
digits in words
»
e.g. entering 123 would produce the output "one two three"
●
inWords
is the method that does the work of translating an integer to
words
public static void
inWords(int numeral)
{
if (numeral < 10)
System.out.print(digitWord(numeral) + " ");
else //numeral has two or more digits
{
inWords(numeral/10);
System.out.print(digitWord(numeral%10) + " ");
}
}
Here is the
recursive call:
inWords
definition calls
itself
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document Chapter 11
●
Each recursive call to
inWords
reduces the integer by one digit
»
it drops out the least significant digit
●
Eventually the argument to
inWords
has only digit
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.
 Spring '08
 durga
 Algorithms, Recursion

Click to edit the document details