This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 subproblems Just return the answer Solve each subproblem j Collect all the solution(s) to the subproblems Use the subsolutions to construct a solution to the complete problem Return the solution by calling some other function on the subproblem j Yes No by calling ourself! SomeOtherFunction (subproblem1) Problem Solving Recursion SomeOtherFunction (subproblemn) ... SolveAProblem (problem) SolveAProblem (subproblem1) SolveAProblem (subproblemn) “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 roughlyequal 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 roughlyequal 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 roughlyequal 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.
 Winter '12
 Smallberg
 Computer Science, Recursion, Sort

Click to edit the document details