{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

10-recursion

# 10-recursion - CSE 143 Lecture 10 Recursion slides created...

This preview shows pages 1–4. Sign up to view the full content.

1 CSE 143 Lecture 10 Recursion slides created by Marty Stepp and Alyssa Harding http://www.cs.washington.edu/143/ 2 Recursion Iteration : a programming technique in which you describe actions to be repeated using a loop Recursion : a programming technique in which you describe actions to be repeated using a method that calls itself • Both approaches can be used to solve the same problems Some problems are easier solved iteratively Some problems are easier solved recursively Sometimes the recursive solution is a LOT simpler than the iterative solution!

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

View Full Document
2 3 Exercise • How many students total are directly behind you in your "column" of the classroom? You have poor vision, so you can see only the people right next to you. So you can't just look back and count. But you are allowed to ask questions of the person next to you. How can we solve this problem? ( recursively ) 4 The idea • Recursion is all about breaking a big problem into smaller occurrences of that same problem. Each person can solve a small part of the problem. • What is a small version of the problem that would be easy to answer? • What information from a neighbor might help me?
3 5 Recursive algorithm • Number of people behind me: If there is someone behind me, ask him/her how many people are behind him/her. • When they respond with a value N , then I will answer N + 1 . If there is nobody behind me, I will answer 0 . 6 Recursion and cases • Every recursive algorithm involves at least 2 cases: base case : A simple occurrence that can be answered directly. recursive case : A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

10-recursion - CSE 143 Lecture 10 Recursion slides created...

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

View Full Document
Ask a homework question - tutors are online