37
Lecture 10 Recursion

Lecture 10 Recursion

  • Upload
    goro

  • View
    42

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 10 Recursion

Lecture 10Recursion

Page 2: Lecture 10 Recursion

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.

Page 3: Lecture 10 Recursion

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 + 15 + 4 + 3 + 35 + 4 + 65 + 1015

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 + 05 + 4 + 3 + 2 + 15 + 4 + 3 + 35 + 4 + 65 + 1015

Page 4: Lecture 10 Recursion

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

Closed-Form Solution for triCount(n)

public static int triCount(int n){ return n*(n+1)/2;}

Page 5: Lecture 10 Recursion

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); }}

Page 6: Lecture 10 Recursion

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) = 2foob(4) = 16foob(5) = 32foob(6) = 64

Page 7: Lecture 10 Recursion

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

Page 8: Lecture 10 Recursion

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) = 1number of calls for furb(4) = 7number of calls for furb(8) = 15number of calls for furb(16) = 31

Page 9: Lecture 10 Recursion

Thinking Recursively

Page 10: Lecture 10 Recursion

Step 1

Page 11: Lecture 10 Recursion

Step 2

Page 12: Lecture 10 Recursion

Step 3

Page 13: Lecture 10 Recursion

Step 4

Page 14: Lecture 10 Recursion

Infinite Recursion

Page 15: Lecture 10 Recursion

The Palindrome Problem

Page 16: Lecture 10 Recursion

Step 1

Page 17: Lecture 10 Recursion

Step 2

Page 18: Lecture 10 Recursion
Page 19: Lecture 10 Recursion

Step 3

Page 20: Lecture 10 Recursion

Step 4

Page 21: Lecture 10 Recursion

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); } } }}

Page 22: Lecture 10 Recursion

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]); } : :}

Page 23: Lecture 10 Recursion

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; } }

Page 24: Lecture 10 Recursion

5 7 2 8 6 3 4 9 1 0Array

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; } }

Page 25: Lecture 10 Recursion

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; } }

0 7 2 8 6 3 4 9 1 5Array

higher

lower

Page 26: Lecture 10 Recursion

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; } }

0 7 2 8 6 3 4 9 1 5Array

higher

lower

Page 27: Lecture 10 Recursion

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; } }

0 5 2 8 6 3 4 9 1 7Array

higher

lower

Page 28: Lecture 10 Recursion

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; } }

0 1 2 8 6 3 4 9 5 7Array

higher

lower

Page 29: Lecture 10 Recursion

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; } }

0 1 2 8 6 3 4 9 5 7Array

higher

lower

Page 30: Lecture 10 Recursion

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; } }

0 1 2 5 6 3 4 9 8 7Array

higher

lower

Page 31: Lecture 10 Recursion

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; } }

0 1 2 5 6 3 4 9 8 7Array

higher

lower

Page 32: Lecture 10 Recursion

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; } }

0 1 2 4 6 3 5 9 8 7Array

higher

lower

Page 33: Lecture 10 Recursion

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; } }

0 1 2 4 6 3 5 9 8 7Array

higher

lower

Page 34: Lecture 10 Recursion

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; } }

0 1 2 4 5 3 6 9 8 7Array

higher

lower

Page 35: Lecture 10 Recursion

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; } }

0 1 2 4 3 5 6 9 8 7Array

higher

lower

Page 36: Lecture 10 Recursion

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; } }

0 1 2 4 3 5 6 9 8 7Array

higher

lower

Page 37: Lecture 10 Recursion

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; } }

0 1 2 4 3 5 6 9 8 7Array

higher

higherlower

lower

0 1 2 4 3 6 9 8 7Array

pivot_index -1 pivot_index -1