Lecture 8 - Recursion

# Lecture 8 - Recursion - • Recursion • How to design an...

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

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

View Full Document

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

View Full Document

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.

Unformatted text preview: • Recursion • How to design an Object Oriented program Monday, February 6th Recursion! Building a SuDoKu solver! Even cracking codes and ciphers! Recursion is one of the most difficult topics in Computer Science… But once you master it, you can solve all sorts of cool problems ! Writing a computer program that can play chess or checkers! Idea Behind Is the problem trivially solved Break the problem into one or more simpler sub-problems Just return the answer Solve each sub-problem j Collect all the solution(s) to the sub-problems Use the sub-solutions to construct a solution to the complete problem Return the solution by calling some other function on the sub-problem j Yes No by calling ourself! SomeOtherFunction (sub-problem1) Problem Solving Recursion SomeOtherFunction (sub-problemn) ... SolveAProblem (problem) SolveAProblem (sub-problem1) SolveAProblem (sub-problemn) “The Lazy Person’s Sort” Let’s design a new sorting algorithm, called the “lazy person’s sort”… Lazy Person’s Sort: Split the cards into two roughly-equal piles Hand one pile to nerdy student A and ask them to sort it Hand the other pile to nerdy student B and ask them to sort it Take the two sorted piles and merge them into a single sorted pile The input to this sort are a bunch of index cards with #s. 6 22 17 3 95 14 6 22 17 3 95 14 “The Lazy Person’s Sort” Lazy Person’s Sort: Split the cards into two roughly-equal piles Hand one pile to nerdy student A and ask them to sort it Hand the other pile to nerdy student B and ask them to sort it Take the two sorted piles and merge them into a single sorted pile 95 22 17 14 6 3 Pretty good! All I had to do was merge two piles of sorted cards! (My nerdy students did all the real work!) Well that worked so well, I think I’ll have them sort the other six hundred! 17 61492 2774 99322 That sucks. Sorting 3 cards was OK… but 300? I don’t know where to start! Yeah right. Hey, you’re kind of cute when you’re angry! <blush>Thanks</blush> But what can we do? I think I have an idea. We can be lazy too, let’s change Carey’s algorithm just a bit. “The Lazy Person’s Sort” Lazy Person’s Sort: Split the cards into two roughly-equal piles Hand one pile to nerdy student A and ask them to sort it Hand the other pile to nerdy student B and ask them to sort it Take the two sorted piles and merge them into a single sorted pile 17 61492 2774 99322 hot say “do the Lazy Person’s Sort” say “do the Lazy Person’s Sort” Oh yeah… One more thing....
View Full Document

## This note was uploaded on 04/02/2012 for the course COMPUTER S 32 taught by Professor Smallberg during the Winter '12 term at UCLA.

### Page1 / 102

Lecture 8 - Recursion - • Recursion • How to design an...

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

View Full Document
Ask a homework question - tutors are online