CH11 - Chapter 11 Recursion Basics of Recursion Programming with Recursion Chapter 11 Java an Introduction to Computer Science Programming Walter

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

View Full Document Right Arrow Icon
Chapter 11 Chapter 11 Basics of Recursion Programming with Recursion 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
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 step-by-step 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
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Image of page 7
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.

Page1 / 29

CH11 - Chapter 11 Recursion Basics of Recursion Programming with Recursion Chapter 11 Java an Introduction to Computer Science Programming Walter

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

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