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

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

View Full Document Right Arrow Icon
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!
Background image of page 1

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

View Full DocumentRight Arrow Icon
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?
Background image of page 2
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.
Background image of page 3

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

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

This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

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 Right Arrow Icon
Ask a homework question - tutors are online