1 / 37

Lecture 10 Recursion

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

goro
Download Presentation

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

  2. 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. * * * * * * * * * * * * * * * publicclass recursionDemo{publicstaticvoid main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5)); }publicstaticint triCount(int n) {if(n==1)return 1;elsereturn triCount(n-1) + n; }} TriCount for n = 5 is... 15

  3. Alternate Versions of triCount(n) publicstaticint triCount(int n){if(n==1)return 1;elsereturn triCount(n-1) + n;} publicstaticint triCount(int n){if(n<=0)return 0;elsereturn 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. Closed-Form Solution for triCount(n) n + (n-1) + (n-2) + . . . + 3 + 2 + 1 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 publicstaticint triCount(int n){return n*(n+1)/2;}

  5. publicclass FactorialDemo{publicstaticvoid 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)); }publicstaticint fact(int n) {if(n<=1)return 1;elsereturn n * fact(n-1); }}

  6. publicclass foobDemo{publicstaticvoid 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)); }publicstaticint foob(int n) {if(n<=0)return 1;elsereturn foob(n-1) + foob(n-1); }} foob(1) = 2foob(4) = 16foob(5) = 32foob(6) = 64

  7. publicclass feebDemo{publicstaticint count;publicstaticvoid 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); }publicstaticint feeb(int n) { count += 1;if(n<=1)return 1;elsereturn feeb(n/2); }} # calls for feeb(1) = 1 # calls for feeb(4) = 3 # calls for feeb(8) = 4 # calls for feeb(256) = 9

  8. publicclass furbDemo{publicstaticint count;publicstaticvoid 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); }publicstaticint furb(int n) { count += 1;if(n<=1)return 1;elsereturn 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

  18. Step 3

  19. Step 4

  20. import java.util.Scanner;publicclass plaindromDemo{publicstaticvoid main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine();if(isPalindrome(str))System.out.println("This is a palindrome");elseSystem.out.println("This is not a palindrome"); }publicstaticboolean isPalindrome(String text)// Separate case for shortest strings. {int length = text.length();if (length <= 1) { returntrue; }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); }elsereturnfalse; }elseif (!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); } } }}

  21. import java.io.IOException;import java.util.Scanner;publicclass QuickSortTest2{ publicstaticvoid 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 = newdouble[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]);} : : }

  22. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }}

  23. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 5 7 2 8 6 3 4 9 1 0 higher lower

  24. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 7 2 8 6 3 4 9 1 5 higher lower

  25. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 7 2 8 6 3 4 9 1 5 higher lower

  26. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 5 2 8 6 3 4 9 1 7 higher lower

  27. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 8 6 3 4 9 5 7 higher lower

  28. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 8 6 3 4 9 5 7 higher lower

  29. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 5 6 3 4 9 8 7 higher lower

  30. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 5 6 3 4 9 8 7 higher lower

  31. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 4 6 3 5 9 8 7 higher lower

  32. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 4 6 3 5 9 8 7 higher lower

  33. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 4 5 3 6 9 8 7 higher lower

  34. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 4 3 5 6 9 8 7 higher lower

  35. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array 0 1 2 4 3 5 6 9 8 7 higher lower

  36. publicstaticdouble[] 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; }publicstaticint 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; }elsereturn higher; }} Array Array 0 0 1 1 2 2 4 4 3 3 5 6 6 9 9 8 8 7 7 higher lower higher lower pivot_index -1 pivot_index -1

More Related