# lecture8b - Introduction to Computer Programming with...

• Test Prep
• 27

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

Introduction to Computer Programming with MATLAB CEE/MAE M20 Lecture 8b Divide and Conquer! CEE/MAE M20 Reading 2 The Manhattan 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? Insight Through Computing An ordered (sorted) list The Manhattan 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? Insight Through Computing

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

Key idea of “phone book search”: repeated halving To find the page containing Pat Reed ’s number… while (Phone book is longer than 1 page) Open to the middle page. if Reed ” comes before the first entry, Rip and throw away the 2 nd half. else Rip and throw away the 1 st half. end end Insight Through Computing What happens to the 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 Insight Through Computing Binary Search 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. Insight Through Computing Binary Search Insight Through Computing
Binary Search 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. “Savings” is significant! Insight Through Computing Binary Search 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. “Savings” is significant! n log2(n) 100 7 1000 10 10000 13 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 Binary search: target x = 70 v L: Mid: R: 1 6 12 1 2 3 4 5 6 7 8 9 10 11 12 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 Binary search: target x = 70 v L: Mid: R: 1 6 12 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x Insight Through Computing

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

12 15 35 33 42 45 51 73 62 75 86 98 Binary search: target x = 70 v L: Mid: R: 1 6 12 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x So throw away the left half… Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 9 12 1 2 3 4 5 6 7 8 9 10 11 12 Binary search: target x = 70 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 9 12 1 2 3 4 5 6 7 8 9 10 11 12 x < v(Mid) Binary search: target x = 70 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 9 12 1 2 3 4 5 6 7 8 9 10 11 12 x < v(Mid) So throw away the right half… Binary search: target x = 70 Insight Through Computing
12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 7 9 1 2 3 4 5 6 7 8 9 10 11 12 Binary search: target x = 70 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 7 9 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x Binary search: target x = 70 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 6 7 9 1 2 3 4 5 6 7 8 9 10 11 12 v(Mid) <= x So throw away the left half… Binary search: target x = 70 Insight Through Computing 12 15 35 33 42 45 51 73 62 75 86 98 v L: Mid: R: 7 8 9 1 2

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

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

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern