Lecture 10 Recursion. public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5));

Slides:



Advertisements
Similar presentations
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Advertisements

Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 13 – Recursion.
Chapter 18 Recursion "To iterate is human, to recurse divine.", L. Peter Deutsch.
CHAPTER 13 Recursion. Recursive Solution A recursive solution  solves a problem by solving a smaller instance of the problem. Example  How do we go.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Recursion!. Can a method call another method? YES.
TA: Nouf Al-Harbi NoufNaief.net :::
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Recursion.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
LAB 10.
Computer Programming Lab(4).
Recursion & Collections API Recursion Revisited Programming Assignments using the Collections API.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 13 - Recursion.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Thirteen: Recursion.
Chapter 5 Case Study. Chapter 5 The RPS Flowchart.
Chapter 18 Recursion. Chapter Goals To learn about the method of recursion To understand the relationship between recursion and iteration To analyze problems.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Miscellaneous topics Chapter 2 Savitch. Miscellaneous topics Standard output using System.out Input using Scanner class.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
The if-else statement. The if-else statement in Java The if-else statement is the second conditional statement in Java The if-else statement selects one.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
Copyright © 2013 by John Wiley & Sons. All rights reserved. RECURSION CHAPTER Slides by Rick Giles 13.
Chapter 4: Control Structures II
1 Traversal algorithms. 2 Array traversal traversal: An examination of each element of an array. Traversal algorithms often takes the following form:
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Starting Out with Java: From Control Structures through Objects 5 th edition By Tony Gaddis Source Code: Chapter 9.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
C OMMON M ISTAKES CSC Java Program Structure  String Methods.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
(Dreaded) Quiz 2 Next Monday.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Chapter 13 Recursion.
Lecture 1 – Java basics.
using System; namespace Demo01 { class Program
Computer Programming Methodology Input and While Loop
Computer Programming Methodology File Input
Repetition.
הרצאה 3 אלמנטים בסיסיים בשפה
Operators Laboratory /11/16.
Recursion For some problems, it’s useful to have a method call itself.
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Introduction to Java Programming
An Introduction to Java – Part I, language basics
Know for Quiz Everything through Last Week and Lab 7
Recursive GCD Demo public class Euclid {
Self study.
class PrintOnetoTen { public static void main(String args[]) {
Lecture Notes – Week 2 Lecture-2
Recursion Problems.
More on iterations using
Computer Science Club 1st November 2019.
Presentation transcript:

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 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.

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) triCount(3) triCount(2) triCount(1) triCount(5) 5 + triCount(4) triCount(3) triCount(2) triCount(1) triCount(0)

n + (n-1) + (n-2) (n-2) + (n-1) + n (n+1) + (n+1) + (n+1) (n+1) + (n+1) n(n+1) so n + (n-1) + (n-2) = n(n+1)/2 Closed-Form Solution for triCount(n) public static int triCount(int n) { return n*(n+1)/2; }

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

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

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

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

Thinking Recursively

Step 1

Step 2

Step 3

Step 4

Infinite Recursion

The Palindrome Problem

Step 1

Step 2

Step 3

Step 4

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

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

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

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

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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } 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; } } Array higher lower Array pivot_index -1