{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# CH11 - Chapter 11 Recursion Basics of Recursion Programming...

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

Chapter 11 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 Java: an Introduction to Computer Science & Programming - Walter Savitch 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
Chapter 11 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 Java: an Introduction to Computer Science & Programming - Walter Savitch 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 » the if/else statement finally executes the base case » and the algorithm terminates
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern