1 / 19

Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

Chapter 13: Recursion. Starting Out with Programming Logic & Design Second Edition by Tony Gaddis. Chapter Topics. 13.1 Introduction to Recursion 13.2 Problem Solving with Recursion 13.3 Examples of Recursive Algorithms. 13.1 Introduction to Recursion.

alink
Download Presentation

Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 13:Recursion Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

  2. Chapter Topics 13.1 Introduction to Recursion 13.2 Problem Solving with Recursion 13.3 Examples of Recursive Algorithms

  3. 13.1 Introduction to Recursion A recursive module is a module that calls itself When this happens, it becomes like an infinite loop because there may be no way to break out Depth of Recursion is the number of times that a module calls itself Recursion should be written so that it can eventually break away This can be done with an If statement

  4. 13.1 Introduction to Recursion

  5. 13.2 Problem Solving with Recursion A problem can be solved with recursion if it can be broken down into successive smaller problems that are identical to the overall problems This process is never required, as a loop can do the same thing It is generally less efficient to use than loops because it causes more overhead (use of system resources such as memory)

  6. 13.2 Problem Solving with Recursion How it works If the problem can be solved now, then the module solves it and ends If not, then the module reduces it to a smaller but similar problem and calls itself to solve the smaller problem A Base Case is where a problem can be solved without recursion A Recursive Case is where recursion is used to solve the problem

  7. 13.2 Problem Solving with Recursion Using recursion to calculate the factorial of a number A factorial is defined as n! whereas n is the number you want to solve 4! or “four factorial” mean 1*2*3*4 = 24 5! or “five factorial” means 1*2*3*4*5 = 120 0! is always 1 Factorials are often solved using recursion

  8. 13.2 Problem Solving with Recursion Continued…

  9. 13.2 Problem Solving with Recursion

  10. 13.2 Problem Solving with Recursion Inside Program 13-3 Inside the function, if n is 0, then the function returns a 1, as the problem is solved Else, Return n * factorial(n-1) is processed and the function is called again While the Else does return a value, it does not do that until the value of factorial(n-1) is solved

  11. 13.2 Problem Solving with Recursion Figure 13-4 The value of n and the return value during each call of the function

  12. 13.3 Examples of Recursive Algorithms Summing a Range of Array Elements with Recursion Continued…

  13. 13.3 Examples of Recursive Algorithms Summing a Range of Array Elements with Recursion

  14. 13.3 Examples of Recursive Algorithms Inside Program 13-4 start and end represent the array range Return array[start] + rangeSum(array, start+1), end) This continuously returns the value of the first element in the range plus the sum of the rest of the elements in the range It only breaks out when start is greater than end start must be incremented

  15. 13.3 Examples of Recursive Algorithms The Fibonacci Series Continued…

  16. 13.3 Examples of Recursive Algorithms The Fibonacci Series

  17. 13.3 Examples of Recursive Algorithms Inside Program 13-5 The Fibonacci numbers are 0,1,1,2,3,5,8,13,21… After the second number, each number in the series is the sum of the two previous numbers The recursive function continuously processes the calculation until the limit is reached as defined in the for loop in the main module

  18. 13.3 Examples of Recursive Algorithms Additional examples that can be solved with recursion The Greatest Common Divisor A Recursive Binary Search The Towers of Hanoi

  19. 13.3 Examples of Recursive Algorithms Recursion vs. Looping Reasons not to use recursion They are certainly less efficient than iterative algorithms because of the overhead Harder to discern what is going on with recursion Why use recursion The speed and amount of memory available to modern computers diminishes the overhead factor The decision is primarily a design choice

More Related