Announcements Last … First … … quiz section … office hour

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
C++ Programming:. Program Design Including
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Recursion.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Binary search example postponed to end of lecture.
Recursion. Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms Lecture.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Data Structures Using C++1 Chapter 6 Recursion. Data Structures Using C++2 Chapter Objectives Learn about recursive definitions Explore the base case.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 15 Recursion INTRODUCTION Recursion is a program-solving technique that expresses the solution of a problem in terms of the solutions of.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Computer Science 101 A Survey of Computer Science QuickSort.
Recursion. 2 Overview  Learn about recursive definitions  Explore the base case and the general case of a recursive definition  Discover recursive.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Recursion.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Recursion DRILL: Please take out your notes on Recursion
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Towers of Hanoi Move n (4) disks from pole A to pole C
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Quicksort
Programming in Java: lecture 10
Recursion &Faster Sorting
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Java Programming: Program Design Including Data Structures
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Quicksort 1.
Chapter 14: Recursion Starting Out with C++ Early Objects
Recursion Output Input
Chapter 4: Divide and Conquer
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
Unit-2 Divide and Conquer
Fundamentals of Programming
CS1120: Recursion.
Chapter 8: Recursion Java Software Solutions
CMSC201 Computer Science I for Majors Lecture 17 – Recursion (cont)
Chapter 8: Recursion Java Software Solutions
Quicksort.
Recursion.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Quicksort.
CS Problem Solving and Object Oriented Programming Spring 2019
Quicksort.
Recursive Function Prepared by Harprith ICT2102 Introduction to Data Structure.
Presentation transcript:

Announcements Last … First … … quiz section … office hour … chance to have your TA autograph your textbook! First … … final of the quarter! In ARCHITECTURE 147 10:30 AM, Wed June 6 … evals of the quarter … quiz section without a quiz!

Recursion Sometimes, it just makes more sense to define a problem in terms of similar subproblems Famous Examples: Fibonnacci numbers Factorial Towers of Hanoi Quicksort Cheesy examples: Calculating the modulus of two numbers Finding the largest element in an array Very famous sorting algorithm in computer science!

Designing Recursive Procedures Hannah’s Never-fail Recursion Recipe Assume you can solve smaller instances of the larger problem. Solve the larger problem by calling the procedure you’re currently designing with smaller inputs. (Remember: you assumed you could solve smaller problems!) Figure out where problem reduction takes you (ie -- what are your base cases), and implement these base cases. Check the base cases before making the recursive calls!

Recursively Sorting an Array The problem: sort an array of size n Assume you can sort arrays smaller than n Pick a random element in the array (we’ll call it the “pivot”) and move all elements smaller than the pivot to its left. All elements larger go to the pivot’s right. Sort the subarray to the left of the pivot (its size is less than n) Sort the subarray to the right of the pivot (its size is less than n) If the size of the arrays we’re sorting keeps shrinking, eventually the size of one array will be 1. Assume you can solve smaller instances of the larger problem. Solve the larger problem by calling the procedure you’re currently designing with smaller inputs. (Remember: you assumed you could solve smaller problems!) Figure out where problem reduction takes you (ie -- what are your base cases), and implement these base cases. Check the base cases before making the recursive calls!

Recursion in C A recursive function calls itself! Recursion is a very powerful tool Some of the fastest sorting algorithms in computer science couldn’t work without recursion (well, sorta. See below) Any iterative problem can be restated in terms of recursion, and vice versa So technically, those fast sorting algorithms could work without recursion. But they wouldn’t be as fast BTW -- the recursive sorting example above was Quicksort, one of the fastest sorting algorithms in computer science!

Infinite Recursion Boo! Hiss!! If: There’s no base case Your base case(s) doesn’t cover all possibilities Then: Infinite recursion: your procedure keeps calling itself over and over and over and over and over and over and over … Example: int modByTwo(int num) { int ans; if(num == 0) return 0; /* What base case did we forget? */ else { ans = modByTwo(num - 2); return ans; } Note: NEVER say “recurse” Recurse is “!@#$ !@#$ !@#$” Recursive procedures recur

Recursion and Programming How does the program know when to stop? When it reaches a “base case(s)” How does the program “keep track” of where in the recursive procedure it is? Remember these two points from lecture: “Every time you call a function, you get a fresh copy of it” “When you exit a function, only that copy of it goes away” When a function is called, a new stack frame is created. Each recursive function call therefore keeps copies of its own local variables. When that function call returns, those local variables are destroyed.

Recursion and the Call Stack int fact(int n) { int temp; /* Base case */ if(n <= 1) return 1; /* Recursive step */ temp = fact (n - 1); return temp * n; } int main(void) { int ans = factorial(3); return 0; temp fact’s stack frame n temp fact’s stack frame n temp fact’s stack frame n main’s stack frame ans