{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Chapter 11 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 11 Basics of Recursion Programming with Recursion Recursion
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern