Download presentation
Presentation is loading. Please wait.
Published byFelix Woods Modified over 9 years ago
1
Lecture 10 Recursion
2
public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5)); } public static int triCount(int n) { if(n==1) return 1; else return triCount(n-1) + n; } } * * * * * * * * * * * TriCount for n = 5 is... 15 TriCount(n) - A Recursion Demo The number of stars in a triangular array of stars with a base width of 1 is equal to 1. The number of stars in a triangular array of stars with a base width n is equal to n + the number of stars in a triangular array of stars with base width n - 1.
3
public static int triCount(int n) { if(n<=0) return 0; else return triCount(n-1) + n; } Alternate Versions of triCount(n) public static int triCount(int n) { if(n==1) return 1; else return triCount(n-1) + n; } triCount(5) 5 + triCount(4) 5 + 4 + triCount(3) 5 + 4 + 3 + triCount(2) 5 + 4 + 3 + 2 + triCount(1) 5 + 4 + 3 + 2 + 1 5 + 4 + 3 + 3 5 + 4 + 6 5 + 10 15 triCount(5) 5 + triCount(4) 5 + 4 + triCount(3) 5 + 4 + 3 + triCount(2) 5 + 4 + 3 + 2 + triCount(1) 5 + 4 + 3 + 2 + 1 + triCount(0) 5 + 4 + 3 + 2 + 1 + 0 5 + 4 + 3 + 2 + 1 5 + 4 + 3 + 3 5 + 4 + 6 5 + 10 15
4
n + (n-1) + (n-2) +... + 3 + 2 + 1 + 1 + 2 + 3 +... + (n-2) + (n-1) + n (n+1) + (n+1) + (n+1) +... + (n+1) + (n+1) n(n+1) so n + (n-1) + (n-2) +... + 3 + 2 + 1 = n(n+1)/2 Closed-Form Solution for triCount(n) public static int triCount(int n) { return n*(n+1)/2; }
5
public class FactorialDemo { public static void main(String[] args) { System.out.println("factorial of 5 = " + fact(5)); System.out.println("factorial 0f 15 = " + fact(15)); System.out.println("factorial of 20 = " + fact(20)); } public static int fact(int n) { if(n<=1) return 1; else return n * fact(n-1); } }
6
public class foobDemo { public static void main(String[] args) { System.out.println("foob(1) = " + foob(1)); System.out.println("foob(4) = " + foob(4)); System.out.println("foob(5) = " + foob(5)); System.out.println("foob(6) = " + foob(6)); } public static int foob(int n) { if(n<=0) return 1; else return foob(n-1) + foob(n-1); } } foob(1) = 2 foob(4) = 16 foob(5) = 32 foob(6) = 64
7
public class feebDemo { public static int count; public static void main(String[] args) { count = 0; feeb(1); System.out.println("# calls for feeb(1) = " + count); count = 0; feeb(4); System.out.println("# calls for feeb(4) = " + count); count = 0; feeb(8); System.out.println("# calls for feeb(8) = " + count); count = 0; feeb(256); System.out.println("# calls for feeb(256) = " + count); } public static int feeb(int n) { count += 1; if(n<=1) return 1; else return feeb(n/2); } } # calls for feeb(1) = 1 # calls for feeb(4) = 3 # calls for feeb(8) = 4 # calls for feeb(256) = 9
8
public class furbDemo { public static int count; public static void main(String[] args) { count = 0; furb(1); System.out.println("number of calls for furb(1) = " + count); count = 0; furb(4); System.out.println("number of calls for furb(4) = " + count); count = 0; furb(8); System.out.println("number of calls for furb(8) = " + count); count = 0; furb(16); System.out.println("number of calls for furb(16) = " + count); } public static int furb(int n) { count += 1; if(n<=1) return 1; else return furb(n/2) + furb(n/2); } } number of calls for furb(1) = 1 number of calls for furb(4) = 7 number of calls for furb(8) = 15 number of calls for furb(16) = 31
9
Thinking Recursively
10
Step 1
11
Step 2
12
Step 3
13
Step 4
14
Infinite Recursion
15
The Palindrome Problem
16
Step 1
17
Step 2
19
Step 3
20
Step 4
21
import java.util.Scanner; public class plaindromDemo { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); if(isPalindrome(str)) System.out.println("This is a palindrome"); else System.out.println("This is not a palindrome"); } public static boolean isPalindrome(String text) // Separate case for shortest strings. { int length = text.length(); if (length <= 1) { return true; } else { char first = Character.toLowerCase(text.charAt(0)); // Get first/last chars, convert to lowercase. char last = Character.toLowerCase(text.charAt(length - 1)); if (Character.isLetter(first) && Character.isLetter(last)) // Both are letters. { if (first == last) { String shorter = text.substring(1, length - 1); // Remove both first and last character. return isPalindrome(shorter); } else return false; } else if (!Character.isLetter(last)) { String shorter = text.substring(0, length - 1); // Remove last character. return isPalindrome(shorter); } else { String shorter = text.substring(1); // Remove first character. return isPalindrome(shorter); } } } }
22
import java.io.IOException; import java.util.Scanner; public class QuickSortTest2 { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); System.out.print("Enter number of values in list... "); int n=input.nextInt(); double[] Array = new double[n]; for(int i=0;i<n;i++) Array[i] = Math.random(); for(int i=0;i<Array.length;i++) System.out.println(Array[i]); quickSort(Array, 0, Array.length - 1); for(int i=0;i<Array.length;i++) System.out.println(Array[i]); } : }
23
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1<higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower]<pivot) lower++; while(array[higher]>pivot) higher--; if(lower<higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } }
24
5728634910 Array higher lower public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } }
25
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0728634915 Array higher lower
26
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0728634915 Array higher lower
27
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0528634917 Array higher lower
28
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0128634957 Array higher lower
29
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0128634957 Array higher lower
30
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0125634987 Array higher lower
31
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0125634987 Array higher lower
32
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0124635987 Array higher lower
33
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0124635987 Array higher lower
34
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0124536987 Array higher lower
35
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0124356987 Array higher lower
36
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 0124356987 Array higher lower
37
public static double[] quickSort(double[] array, int lower, int higher)throws IOException { if(lower<higher) { int pivot_index = partition(array,lower,higher); if(pivot_index > 1) quickSort(array, lower, pivot_index - 1); if(pivot_index+1 < higher) quickSort(array, pivot_index + 1, higher); } return array; } public static int partition(double[] array, int lower, int higher) { double pivot=array[lower]; while(true) { while(array[lower] < pivot) lower++; while(array[higher] > pivot) higher--; if(lower < higher) { double temp=array[higher]; array[higher]=array[lower]; array[lower]=temp; } else return higher; } } 01243 5 6987 Array higher lower 012436987 Array pivot_index -1
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.