{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# CarrCh10v2 - C H A P T E R 10 Recursion CONTENTS What Is...

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

223 C H A P T E R 10 Recursion C ONTENTS What Is Recursion? Tracing a Recursive Method Recursive Methods That Return a Value Recursively Processing an Array Recursively Processing a Linked Chain The Time Efficiency of Recursive Methods The Time Efficiency of countDown The Time Efficiency of Computing x n A Simple Solution to a Difficult Problem A Poor Solution to a Simple Problem Tail Recursion Mutual Recursion P REREQUISITES Chapter 1 Java Classes Chapter 5 List Implementations That Use Arrays Chapter 6 List Implementations That Link Data Chapter 9 The Efficiency of Algorithms O BJECTIVES After studying this chapter, you should be able to Determine whether a given recursive method will end successfully in a finite amount of time Write a recursive method Estimate the time efficiency of a recursive method Identify tail recursion and replace it with iteration R epetition is a major feature of many algorithms. In fact, repeating things rapidly is a key ability of computers. Two problem-solving processes involve repetition; they

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

View Full Document
224 CHAPTER 10 Recursion are called iteration and recursion. In fact, most programming languages provide two kinds of repetitive constructs, iterative and recursive. You know about iteration because you know how to write a loop. Regardless of the loop con- struct you use— for , while , or do —your loop contains the statements that you want to repeat and a mechanism for controlling the number of repetitions. You might have a counted loop that counts repetitions as 1, 2, 3, 4, 5, or 5, 4, 3, 2, 1. Or the loop might execute repeatedly while a boolean variable or expression is true. Iteration often provides a straightforward and efficient way to imple- ment a repetitive process. At times, iterative solutions are elusive or hopelessly complex. Discovering or verifying such solutions is not a simple task. In these cases, recursion can provide an elegant alternative. Some recursive solutions can be the best solutions, some provide insight for finding a better iterative solu- tion, and some should not be used at all because they are grossly inefficient. Recursion, however, remains an important problem-solving strategy. This chapter will show you how to think recursively. What Is Recursion? 10.1 You can build a house by hiring a contractor. The contractor in turn hires several subcontractors to complete portions of the house. Each subcontractor might hire other subcontractors to help.You use the same approach when you solve a problem by breaking it into smaller problems. In one special variation of this problem-solving process, the smaller problems are identical except for their size. This special process is called recursion. Suppose that you can solve a problem by solving an identical but smaller problem. How will you solve the smaller problem? If you use recursion again, you will need to solve an even smaller problem that is just like the original problem in every other respect. How will replacing a problem with another one ever lead to a solution? One key to the success of recursion is that eventually you
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}