CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Recursion CS 367 – Introduction to Data Structures.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Recursion Introduction to Computing Science and Programming I.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Searching Arrays Linear search Binary search small arrays
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Stacks CISC181 Spring 2004 P. T. Conrad University of Delaware.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
Problem Solving and Algorithms
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
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.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
February 4, 2005 Searching and Sorting Arrays. Searching.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
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.
CSIS 123A Lecture 9 Recursion Glenn Stevenson CSIS 113A MSJC.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Function Recursion to understand recursion you must understand recursion.
Recursion – some examples. Sum of Squares Write Vertical.
Programming Abstractions Cynthia Lee CS106X. Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Programming Abstractions
to understand recursion you must understand recursion
Programming Abstractions
COMP 53 – Week Seven Big O Sorting.
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to C++ Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Programming Abstractions
Programming Abstractions
Programming Abstractions
Programming Abstractions
Programming Abstractions
Chapter 17 Recursion.
Presentation transcript:

CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

CS106X: Today’s Topics Recursion!!! 1. Factorial 2. Computer memory 3. Binary search 2

Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of our recursion examples….

Factorial! Recursive definition  n ! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } What is the third thing printed when we call factorial(10)? A.2 B.3 C.7 D.8 E.Other/none/more

Factorial! Recursive definition  n ! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code What is the second value ever returned when we call factorial(10)? A.1 B.2 C.10 D.90 E.Other/none/more than one long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; }

Factorial! Recursive definition  n ! = if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! Recursive code What is the fourth value ever returned when we call factorial(10)? A.4 B.8 C.24 D.90 E.Other/none/more than one long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; }

How does this look in memory? Memory Heap Stack 0

How does this look in memory? Memory Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 10; long xfac = 0; xfac = factorial(x); } main() Heap mymethod() x: xfac: factorial() n:

How does this look in memory? Memory main() Heap mymethod() x: xfac: factorial() n: 10 0 factorial() n: 9 Memory main() Heap mymethod() x: xfac: factorial() n: Memory main() Heap mymethod() x: xfac: factorial() n:

The “stack” part of memory is a stack Function call = push Return = pop

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Return 1 Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Return 2 Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Return 6 Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: Return 24 Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

The “stack” part of memory is a stack main() Heap mymethod() x: xfac: 4 24 Recursive code long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } void myfunction(){ int x = 4; long xfac = 0; xfac = factorial(x); }

Factorial! Iterative version long factorial(int n) { long f = 1; while ( n > 1 ) { f = f * n; n = n – 1; } return f; } Recursive version long factorial ( int n ) { cout << n << endl; long result; if (n==1) result = 1; else result = n*factorial(n–1); return result; } NOTE: sometimes iterative can be much faster because it doesn’t have to push and pop stack frames. Method calls have overhead in terms of space and time to set up and tear down.

Classic CS problem: searching

Imagine storing sorted data in an array  How long does it take us to find a number we are looking for?

Imagine storing sorted data in an array  How long does it take us to find a number we are looking for?  If you start at the front and proceed forward, each item you examine rules out 1 item

Imagine storing sorted data in an array  If instead we jump right to the middle, one of three things can happen: 1. The middle one happens to be the number we were looking for, yay! 2. We realize we went too far 3. We realize we didn’t go far enough

Imagine storing sorted data in an array  If instead we jump right to the middle, one of three things can happen: 1. The middle one happens to be the number we were looking for, yay! 2. We realize we went too far 3. We realize we didn’t go far enough Ruling out HALF the options in one step is so much faster than only ruling out one!

Binary search  Let’s say the answer was 3, “we didn’t go far enough”  We ruled out the entire first half, and now only have the second half to search  We could start at the front of the second half and proceed forward…

Binary search  Let’s say the answer was 3, “we didn’t go far enough”  We ruled out the entire first half, and now only have the second half to search  We could start at the front of the second half and proceed forward…but why do that when we know we have a better way?  Jump right to the middle of the region to search

Binary search  Let’s say the answer was 3, “we didn’t go far enough”  We ruled out the entire first half, and now only have the second half to search  We could start at the front of the second half and proceed forward…but why do that when we know we have a better way?  Jump right to the middle of the region to search RECURSION !!

To write a recursive function, we need base case(s) and recursive call(s)  What would be a good base case for our Binary Search function? A. Only three items remain: save yourself an unnecessary function call that would trivially divide them into halves of size 1, and just check all three. B. Only two items remain: can’t divide into two halves with a middle, so just check the two. C. Only one item remains: just check it. D. No items remain: obviously we didn’t find it. E. More than one

[Remainder of slides are for Monday 1/20]

Binary Search bool binarySearch(Vector data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector data, int key, int start, int end){ // Write code to handle the base case “No items remain: // obviously we didn’t find it” by returning false (A) if (data.size() <= 0) return false; (B) if (end-start <= 0) return false; (C) Other/none/more //to be continued… }

Binary Search bool binarySearch(Vector data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector data, int key, int start, int end){ if (end-start <= 0) return false; int middle = start + (end-start)/2; if (data[middle] == key) return true; else if (data[middle] > key) { (A) return binarySearch(data,key,middle+1,end); (B) return binarySearch(data,key,middle,end-1); (C) return binarySearch(data,key,start,middle-1); (D) Other/none/more } // to be continued… }

Binary Search bool binarySearch(Vector data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector data, int key, int start, int end){ if (end-start <= 0) return false; int middle = start + (end-start)/2; if (data[middle] == key) return true; else if (data[middle] > key) { return binarySearch(data,key,start,middle-1); } else { return binarySearch(data,key,middle+1,end); }