Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class 2: Recursion (1). cis 335 Fall 2001 Barry Cohen Iterative problem solving n “Begin at the beginning.” n “Continue until you come to the end.” n.

Similar presentations


Presentation on theme: "Class 2: Recursion (1). cis 335 Fall 2001 Barry Cohen Iterative problem solving n “Begin at the beginning.” n “Continue until you come to the end.” n."— Presentation transcript:

1 Class 2: Recursion (1)

2 cis 335 Fall 2001 Barry Cohen Iterative problem solving n “Begin at the beginning.” n “Continue until you come to the end.” n “Then stop.”

3 cis 335 Fall 2001 Barry Cohen Recursion: Divide and conquer n Express the solution in terms of smaller problems of the same type. (Dividing.) n If the problem is small enough (the ‘base case’), give the answer. (Conquest.) n Make sure that dividing leads to the base case.

4 cis 335 Fall 2001 Barry Cohen Look up phone # of John Doe n How many pages to search? n Look at first page. n If John’s name is on the page, you’re done. n Else, search next page. n How much smaller does the problem become with each step?

5 cis 335 Fall 2001 Barry Cohen Recursive phone # lookup n How many pages to search? n Look at middle page. n If John’s name is on the page, you’re done. n Else, search the half containing ‘Doe’. n How much smaller does the problem become with each step?

6 cis 335 Fall 2001 Barry Cohen Tracing a recursion n Each step in the recursion is a separate box n Track the local variables, function value and the parameters. n A box may have one or more ‘child’ boxes.

7 cis 335 Fall 2001 Barry Cohen Recursion pluses and minuses n + Often easy to write n + Sometimes very efficient n - Takes getting used to. n - May be inefficient n + May lead to a good iterative solution


Download ppt "Class 2: Recursion (1). cis 335 Fall 2001 Barry Cohen Iterative problem solving n “Begin at the beginning.” n “Continue until you come to the end.” n."

Similar presentations


Ads by Google