1 / 11

Recursion

This text explains the concepts of recursion and iteration in computer programming, focusing on the factorial problem. It provides code examples for both recursive and iterative factorial algorithms, along with a comparison of their usage. The text also discusses linear recursion, tail recursion, higher-order recursion, and the rules of recursion. Additionally, it presents examples of using tail recursion to convert an array into its absolute values, a binary recursive search algorithm, and a recursive program to generate a specific output pattern.

junec
Download Presentation

Recursion

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. Recursion

  2. Recursion • Idea: Some problems can be broken down into smaller versions of the same problem • Example: n! • 1*2*3*…*(n-1)*n • n*factorial of (n-1)

  3. Base Case • 5! = 5*4! • 4! = 4*3! • 3! = 3*2! • 2! = 1*1! • 1! = 1 Base case – you always need a terminating condition to end

  4. Function: factorial int factorial(int n) {if(n == 1) return 1; else return (n*factorial(n-1)); }

  5. Iterative Factorial int factorial(int n) {int i; int product = 1; for(i = n; i > 1; i--) { product = product * i; } return product; }

  6. Comparison • Why use iteration over recursion or vice versa?

  7. Linear Recursion • At most 1 recursive call at each iteration • Example: Factorial • General algorithm • Test for base cases • Recurse • Make 1 recursive call • Should make progress toward the base case • Tail recursion • Recursive call is last operation • Can be easily converted to iterative

  8. Higher-Order Recursion • Algorithm makes more than one recursive call • Binary recursion • Halve the problem and make two recursive calls • Example? • Multiple recursion • Algorithm makes many recursive calls • Example?

  9. Rules of Recursion • Base cases. You must always have some bases cases, which can be solved without recursion. • Making progress. For the cases that are to be solved recursively, the recursive call must always be to a case that makes progress toward a base case. • Design rule. Assume that all the recursive calls work. • Compound interest rule. Never duplicate work by solving the same instance of a problem in separate recursive calls.

  10. Examples • Give an algorithm that uses tail recursion to convert an array of elements into their absolute value. • Describe a binary recursive method for search for an element X in an unsorted array A.

  11. Example • Design a recursive program to produce the following output: 0 0 1 0 1 2 0 1 2 3 0 1 2 3 4 0 1 2 3 0 1 2 0 1 0

More Related