Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Last … First … … quiz section … office hour

Similar presentations


Presentation on theme: "Announcements Last … First … … quiz section … office hour"— Presentation transcript:

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

2 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!

3 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!

4 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!

5 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!

6 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

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

8 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


Download ppt "Announcements Last … First … … quiz section … office hour"

Similar presentations


Ads by Google