CS 100 M Lecture 24

# CS 100 M Lecture 24 - 24. Divide and Conquer Algorithms...

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

24. Divide and Conquer Algorithms Binary Search Merge Sort Mesh Generation Recursion

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

View Full Document
Question The Manhatten phone book has 1,000,000+ entries. How is it possible to locate a name by examining just a tiny , tiny fraction of those entries?
Answer: Repeated Halving To find the page containing Derek Jeter’s number… while (Phone book is longer than 1 page) Open to the middle page. if “Jeter” comes before the first name, Rip and throw away the 2 nd half. else Rip and throw away the 1 st half. end end

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

View Full Document
What Happens to Phone Book Length… Original: 3000 pages After 1 rip: 1500 pages After 2 rips: 750 pages After 3 rips: 375 pages After 4 rips: 188 pages After 5 rips: 94 pages After 12 rips: 1 page
Binary Search The idea of repeatedly halving the size of the “search space” is the main idea behind the method of binary search. An item in a sorted array of length n can be located with just log 2 n comparisons.

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

View Full Document
Problem Search a Sorted Array For a Given Value (We assume that the array has no repeated elements.)
12 15 35 33 42 45 51 73 62 75 86 98 Binary Search: a = 70 x: L: Mid: R: 1 6 12 1 2 3 4 5 6 7 8 9 10 11 12 x(Mid) <= a So throw away the left half…

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

View Full Document
12 15 35 33 42 45 51 73 62 75 86 98 Binary Search: a = 70 x: L: Mid: R: 6 9 12 1 2 3 4 5 6 7 8 9 10 11 12 a < x(Mid) So throw away the right half…
12 15 35 33 42 45 51 73 62 75 86 98 Binary Search: a = 70 x: L: Mid: R: 6 7 9 1 2 3 4 5 6 7 8 9 10 11 12 x(Mid) <= a So throw away the left half…

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

View Full Document
12 15 35 33 42 45 51 73 62 75 86 98 Binary Search: a = 70 x: L: Mid: R: 7 8 9 1 2 3 4 5 6 7 8 9 10 11 12 x(Mid) <= a So throw away the left half…
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/23/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell University (Engineering School).

### Page1 / 59

CS 100 M Lecture 24 - 24. Divide and Conquer Algorithms...

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

View Full Document
Ask a homework question - tutors are online