Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 http://peerinstruction4cs.org.Cynthia Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

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

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

4 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

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

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

7 How does this look in memory? Memory Heap Stack 0

8 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: 10 0 0

9 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: 9 10 0 Memory main() Heap mymethod() x: xfac: factorial() n: 10 0 9

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

11 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 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); }

12 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 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); }

13 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 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); }

14 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 2 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); }

15 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 2 1 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); }

16 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 2 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); }

17 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 3 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); }

18 The “stack” part of memory is a stack main() Heap mymethod() x: xfac: factorial() n: 4 4 0 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); }

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

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

21 Classic CS problem: searching

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

23 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 012345678910 2781325293351899095

24 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 012345678910 2781325293351899095

25 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! 012345678910 2781325293351899095

26 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… 012345678910 2781325293351899095

27 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 012345678910 2781325293351899095

28 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 012345678910 2781325293351899095 RECURSION !!

29 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

30 [Remainder of slides are for Monday 1/20]

31 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… }

32 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… }

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


Download ppt "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."

Similar presentations


Ads by Google