Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.

Slides:



Advertisements
Similar presentations
Basics of Recursion Programming with Recursion
Advertisements

C++ Programming:. Program Design Including
Recursion Chapter 11. Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar with the binary.
Intro to CS – Honors I Merge Sort GEORGIOS PORTOKALIDIS
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Programming with Recursion. Example of Recursion (Ask Until the User Gets It Right) import java.util.* ; public class CountDown { private int count; public.
Recursion.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search ; Reading p Selection Sort ; Reading p
CS180 RECURSION March 28,2008. Announcements Project 7 : Recursive Expression Evaluators Milestone Due : 4/2/2008 Project Due : 4/9/2008 Exam 2 to be.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion.
Building Java Programs Chapter 13 Searching reading: 13.3.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Searching and Sorting Linear Search Binary Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Chapter 13 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Recursive Functions for Tasks(13.1) Recursive Functions.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Recursion Powerful Tool
Recursion.
Using recursion for Searching and Sorting
Chapter 15 Recursion.
Chapter 15 Recursion.
Java 4/4/2017 Recursion.
Recursion Chapter 10.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Chapter 11.
Announcements Final Exam on August 19th Saturday at 16:00.
Unit 3 Test: Friday.
Recursion Chapter 11.
Module 1-10: Recursion.
Basics of Recursion Programming with Recursion
Java Programming: Chapter 9: Recursion Second Edition
Search,Sort,Recursion.
Recursion Chapter 11.
Chapter 18 Recursion.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Announcements/Reminders l Project 7 due on Thursday April 14 l Exam 2 »Returned today »Average: ~ 72.5

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Recursive Methods Must Eventually Terminate A recursive method must have at least one base, or stopping, case. l A base case does not execute a recursive call »it stops the recursion l Each successive call to itself must be a "smaller version of itself" so that a base case is eventually reached »an argument must be made smaller each call so that eventually the base case executes and stops the recursion

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Remember : Key to Successful Recursion Recursion will not work correctly unless you follow some specific guidelines: The heart of the method definition can be an if-else statement or some other branching statement. l One or more of the branches should include a recursive invocation of the method. »Recursive invocations should use "smaller" arguments or solve "smaller" versions of the task. l One or more branches should include no recursive invocations. These are the stopping cases or base cases.

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 5 Example: a Recursive Algorithm One way to search a phone book (which is an alphabetically ordered list) for a name is with the following recursive algorithm: Search : middle page = (first page + last page)/2 Open the phone book to middle page; If (name is on middle page) then done; //this is the base case else if (name is alphabetically before middle page) last page = middle page //redefine search area to front half Search //recursive call with reduced number of pages else //name must be after middle page first page = middle page //redefine search area to back half Search //recursive call with reduced number of pages

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Binary Search Code The find method of ArraySearcher implements a binary search algorithm l It returns the index of the entry if the target value is found or -1 if it is not found l Compare it to the pseudocode for the "name in the phone book" problem

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch Indexes Contents target is 33 The array a looks like this: Binary Search Example mid = (0 + 9) / 2 (which is 4 ) 33 > a[mid] (that is, 33 > a[4] ) So, if 33 is in the array, then 33 is one of: Eliminated half of the remaining elements from consideration because array elements are sorted.

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch Indexes Contents target is 33 The array a looks like this: Binary Search Example mid = (5 + 6) / 2 (which is 5 ) 33 == a[mid] So we found 33 at index 5 : 33 5 mid = (5 + 9) / 2 (which is 7 ) 33 < a[mid] (that is, 33 < a[7] ) So, if 33 is in the array, then 33 is one of: Eliminate half of the remaining elements

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Work Examples on Board... l Search for 55 (not in array!)

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Factorial l The factorial of n is defined as: n! = 1 * 2 * 3 *... * (n-1) * n for n > 0 l E.g., 4! = 1 * 2 * 3 * 4 = 24 l What is a recursive definition for n! ? l Write a Java method to compute n!

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Factorial public static int factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); }

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Local Variables l When tracing recursive methods, it is important to remember that each call creates a copy of all local variables (this includes parameters and variables declared inside the recursive method). l Thus, local variables may have different values at each call!

public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2)

public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1)

public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=1 Return 1

public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } n=2 factorial(1) As we saw in the previous slide, this returns 1 n is still 2 in this call, so this method returns 2 * 1

public static void factorial (int n) { if (n==1) return 1; else return n * factorial(n-1); } In another method: int i = factorial(3); n=3 factorial(2) As we saw in the previous slide, this returns 2 n is still 3 in this call, so this method returns 3 * 2 When this method returns, i will be 6

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 18 Fibonacci Sequence l Definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) + fib(n-2) l The sum of the n-th Fibonacci number is the sum of the previous two. l The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21,... l Write a recursive Java method for Fibonacci sequence (Notice that this problem is actually easier with recursion!)

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 19 Fibonacci Sequence public static int fibonacci (int n) { if (n==0) return 0; if (n==1) return 1; else return (fibonacci(n-1) + fibonacci(n-2)); }

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 20 Merge Sort— A Recursive Sorting Method l Example of divide and conquer algorithm l Divides array in half and sorts halves recursively l Combines two sorted halves Merge Sort Algorithm to Sort the Array a If the array a has only one element, do nothing (stopping case). Otherwise, do the following (recursive case): Copy the first half of the elements in a to a smaller array named front. Copy the rest of the elements in the array a to another smaller array named tail. Sort the array front with a recursive call. Sort the array tail with a recursive call. Merge the elements in the arrays front and tail into the array a.

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 21 Merge Sort: sort method public static void sort(int[] a) { if (a.length >= 2) { int halfLength = a.length / 2; int[] front = new int[halfLength]; int[] tail = new int[a.length – halfLength]; divide(a, front, tail); sort(front); sort(tail); merge(a, front, tail); } // else do nothing. } recursive calls make "smaller" problems by dividing array base case: a.length == 1 so a is sorted and no recursive call is necessary. do recursive case if true, base case if false

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 22 Merge Sort: divide method private static void divide (int[] a, int[] front, int[] tail) { int i; for(i=0; i<front.length; i++) front[i] = a[i]; for(i=0; i<tail.length; i++) tail[i] = a[front.length+i]; }

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 23 Merge Sort: merge method private static void merge (int[] a, int[] front, int[] tail) { int frontIndex=0, tailIndex=0, aIndex=0;

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 24 Merge Sort: merge method (cont) while ( (frontIndex < front.length) && (tailIndex < tail.length) ) { if (front[frontIndex] < tail[tailIndex]) { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } else { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; }

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 25 Merge Sort: merge method (cont) while (frontIndex < front.length) { a[aIndex] = front[frontIndex]; aIndex++; frontIndex++; } while (tailIndex < tail.length) { a[aIndex] = tail[tailIndex]; aIndex++; tailIndex++; } } // end merge

Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 26 Summary l If a method definition includes an invocation of the very method being defined, the invocation is called a recursive call. l Recursive calls are legal in Java and sometimes can make code easier to read. l To avoid infinite recursion, a recursive method definition should contain two kinds of cases: one or more recursive calls and one or more stopping cases that do not involve any recursive calls. l Recursion can be a handy way to write code that says "if there is a problem then start the whole process over again."