Presentation is loading. Please wait.

Presentation is loading. Please wait.

From Recursion To Iteration: A Case Study

Similar presentations


Presentation on theme: "From Recursion To Iteration: A Case Study"— Presentation transcript:

1 From Recursion To Iteration: A Case Study
COMP 212 Spring 2007

2 ASorter’s sort() method
public final void sort(Object[] A, int lo, int hi) { if (lo < hi) { int s = split(A, lo, hi); sort(A, lo, s-1); sort(A, s, hi); join(A, lo, s, hi); }

3 A Pathological Case size = n A: sort sort size = n -1 size = 1 sort

4 How Does Recursion Work?
Method Execution Stack main Stack frame: sort sort sort Method parameters, Local variables, Who called whom split

5 Solutions? Tail-recursion elimination Iteration

6 Tail Recursion int fibHelper(int i, int current, int next) {
if (i == 0) return current; else return fib(i - 1, next, current + next); } int fib(int i) { return fibHelper(I, 0, 1);


Download ppt "From Recursion To Iteration: A Case Study"

Similar presentations


Ads by Google