1 / 16

Understanding Recursion and Recursive Functions

This article delves into the concept of recursion, explaining its use in programming and providing examples of recursive functions. It also explores various applications of recursion, such as in mathematical sets and sorting algorithms.

umana
Download Presentation

Understanding Recursion and Recursive Functions

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

  2. What’s the answer? • What value is returned by the following recursive function Mystery for an input value of n? Mystery (integer n) If n = 1 then return 1 else return Mystery(n-1) + 1 end if end function Mystery

  3. Answer • Mystery(n) = n

  4. What does this iterative method do? public static void mystery (String s) { for (int j = s.length() – 1; j >= 0; j --) System.out.print (s.charAt(j)); } // How would we write it recursively?

  5. Recursive version

  6. What is the output? public static void mystery (int n) { if (n < 1) { System.out.print (n); } else { mystery(n/2); System.out.print (”, “ + n); } } // end mystery • When call is • mystery(1) mystery(2) mystery(3) mystery(4) mystery(7) mystery(30) • RUN CODE ON NEXT SLIDE TO SEE ANSWERS.

  7. public class Nov53 { public static void main (String [] args) { mystery(1); System.out.println (); mystery(2); System.out.println (); mystery(3); System.out.println (); mystery(4); System.out.println (); mystery(7); System.out.println (); mystery(30); System.out.println (); } // end main public static void mystery (int n) { if (n < 1) System.out.print (n); else { mystery(n/2); System.out.print (", " + n); } } // end mystery }

  8. Examples • A collection of M numbers is defined recursively by • 2 and 3 belong to M • If X and Y belong to M, so does X*Y Which of the following belong to M? 6 9 16 21 26 54 72 218

  9. Recursively defined sets of strings • A collection S of strings of characters is defined recursively by • a and b belong to S • If X belongs to S, so does Xb Which of the following belong to S? a ab aba aaab bbbbb

  10. Selection Sort • Simulate the execution of algorithm SelectionSorton the following list L; write the list after every exchange that changes the list. • 4, 10, -6, 2, 5

  11. Answer • 4 10 -6 2 5 • 4 5 -6 2 10 • 4 2 -6 5 10 • -6 2 4 5 10

  12. Binary Search • The binary search algorithm is used with the following list; x has the value “flour” • Name the elements against which x is compared. • butter, chocolate, eggs, flour, shortening, sugar

  13. Algorithm for recursive Binary Search BinarySearch (list L; integer i; integer j, itemtype x) // searches sorted list L from L[i] to l[j] for item x If i > j then write (“not found”) else find the index k of the middle item in the list L[i] to L[j] if x = middle item then write (“found”) else if x < middle item then BinarySearch (L, I k-1, x) else BinarySearch (L, k+1, j, x) end if end if end if end function BinarySearch

  14. Recursion with logical formulas • Rule 1: Any statement letter is a wff • Rule 2: If P and Q are wffs, so are (P Λ Q), (P V Q), (P → Q), P’ , and (P ↔ Q) • Example • A, B, C are all wffs by rule 1 • (A Λ B) and (C’) are both wffs by rule 2 • ((A Λ B) → (C’)) is a wff, by rule 2 • (((A Λ B) → (C’))’) • ((A Λ B) → C’)’

  15. Recursion in family relations • James’s parents are ancestors of James

  16. System.out.println (); • A computer virus that spreads by way of e-mail messages is planted in 3 machines the first day. Each day, each infected machine from the day before infects 5 new machines. On the second day, a software solution is found to counteract the virus, and 1 machine is cleaned. Each day thereafter, 6 times as many machines are cleaned as were cleaned the day before. How many days will it be before the effects of the virus are completely gone?

More Related