CS112_10_Slides-RecursiveProcedures

CS112_10_Slides-RecursiveProcedures - Recursive Procedures...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursive Procedures Yinglin Wang CS Dept., SJTU
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1. Review of Recursion definition 2. Review of The pitfalls you should avoid 3. The comparison of recursion and iteration 4. Recursive procedures Contents of this lecture:
Background image of page 2
C) Recursion is when a method either directly or indirectly makes a call to itself. B) Recursion - The process of a function calling itself. (short definition) 1. Several Definition of Recursion: D) Recursion -The process of a function calling itself or calling a sequence of other functions, one of which eventually calls the first function again. (precise definition) A) recursion is a solution technique in which large problems are solved by reducing them to smaller problems of the same form .
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursive problems are like russiandolls: •each large instance contains a similar, slightly smaller one •there is a smallest one that does not contain any dolls Visualization of Recursive Problems
Background image of page 4
2. Pitfalls You Should Avoid When Using Recursion 1. No or missed base case If the base case is not reached the recursion will not stop, and the stack of function calls will exhaust the resources of the machine. This mistake is not caught by the compiler, it is a logical error that causes a run-time error. 2. Incorrect recursive decomposition. The function will compute the wrong value. This is a logical error.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int RaiseToPower(int n, int k) { int i; int result = 1; for(i=0; i<k; i++) { result *= n; } return result; } 3. Comparison: Recursion Vs. Iteration Question: Is it possible to write an iterative version for every recursive function? Answer: yes –true. It is possible to write an iterative version for every recursive function. But sometimes the recursive version is very simple whereas the iterative function for the same task is more complicated. e.g., there is a different way to compute n k that seems much simpler:
Background image of page 6
Q: Are recursive functions more efficient than iterative (looping) ones? A: It is hard to say because it depends many factors. But often the iterative solution is the better solution in terms of performance. - Usually calling a function takes time because copies of all variables and arguments used in the function are pushed onto the call stack, and popped off again at the end of the function. - if run with large arguments, recursive functions also cause space problems: each copy of the arguments and variables of a function on the call stack eats up space. But sometimes it is easier to find the solution using recursion. Recursion Vs. Iteration: Performance Issues
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Problem 1: Write a recursion function find the minimum in an array of length n. Class exercises Problem 2: Write a recursion function to sum the elements of an integer array.
Background image of page 8
int arrayMin(int *ar, int len) { int restmin; if(len ==1) { return ar[0]; //base case } else { restmin = arrayMin(ar, len-1); //recursive call if(ar[len-1] < restmin) { return ar[len-1]; return restmin; } } } idea: to compute the minimum of a[0], a[1], . .., a[n-1]: if (a[n-1] < minimum of (a[0], a[1], . ..,a[n-2]) return a[n-1] else return minimum of a[0], a[1],. ..,a[n-2] What’ s the base case? –an array with one element solution to Problem 1: Write a recursion function find the minimum in an array of length n.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 52

CS112_10_Slides-RecursiveProcedures - Recursive Procedures...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online