Download presentation
Presentation is loading. Please wait.
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); mystery(3); mystery(4); mystery(7); mystery(30); } // 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 SelectionSort on the following list L; write the list after every exchange that changes the list. 4, 10, -6, 2, 5
11
Answer
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”) if x < middle item then BinarySearch (L, I k-1, x) BinarySearch (L, k+1, j, x) 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 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.