CSC151 2010S, Class 46: Binary Search Overview: * The problem of searching. * Analyzing algorithmic efficiency. * Making searching more efficient using divide-and-conquer. * Lab! Admin: * It's a lecture-recitation day! * There is no reading for tomorrow. Spend the extra time working on your project. * EC for Thursday's convocation! * EC for Friday's CS Extra Table. * Exam 3 to be distributed Friday (or maybe Thursday). The problem of searching * Broad goal in CS: Find problems that occur in many contexts and write algorithms that work in those many contexts. * When you find a common problem, it's worth a lot of effort making your solution efficient * It helps other people * It gets used a lot * Searching: Given a collection of values, find one (or all) that meet some criteria * Had you seen a searching algorithm before this reading? * We've done lots of searching through lists * Association lists provide one mechanism for searching * How does assoc work? Assoc searches through a "table" to find something that matches * We implement the table with a list of lists (assoc key table) * If the table is empty, return #f * If the car of the car of table is key, return the car of table * O/w, try again on the cdr of table * Generalizing assoc * One technique: Permit more complex key computations (search key extract-key table) * Another technique: Permit multiple keys * Another technique: (search predicate table) How efficient is the "look at one thing at a time?", given N things in the table * Worst case? N * Average case? N/2, but depends on what you consider a typical search Can we do better? * If we start with a table that is "sorted", * arranged according to some order, e.g., a's come before b's come before c's come before q's * We might have a conception of where something will be * Traditional general strategy: Divide in half, look in the middle, throw half away * Seems to be much faster * Approximately log_2(N) steps * What is 2^N Something to search (define objects-by-name (vector (list "Amy" "ellipse" "blue" 90 50 25 5) (list "Bob" "ellipse" "indigo" 80 40 35 30) (list "Charlotte" "rectangle" "blue" 0 40 5 45) (list "Danielle" "rectangle" "red" 0 140 35 15) (list "Devon" "rectangle" "yellow" 80 0 10 5) (list "Erin" "ellipse" "orange" 60 50 10 15) (list "Fred" "ellipse" "black" 0 110 30 30) (list "Greg" "ellipse" "orange" 110 10 35 50) (list "Heather" "rectangle" "white" 100 140 35 50) (list "Ira" "ellipse" "red" 100 100 5 50) (list "Janet" "ellipse" "black" 60 70 5 20) (list "Karla" "ellipse" "yellow" 20 110 25 10) (list "Leo" "rectangle" "yellow" 60 40 30 50) (list "Maria" "ellipse" "blue" 30 10 5 50) (list "Ned" "rectangle" "yellow" 0 50 45 15) (list "Otto" "rectangle" "red" 100 40 10 20) (list "Paula" "ellipse" "orange" 100 20 50 25) (list "Quentin" "ellipse" "black" 40 130 35 50) (list "Rebecca" "rectangle" "green" 110 70 25 35) (list "Sam" "ellipse" "white" 20 120 35 40) (list "Ted" "rectangle" "black" 20 0 10 20) (list "Urkle" "rectangle" "indigo" 40 110 10 5) (list "Violet" "rectangle" "violet" 80 80 50 20) (list "Xerxes" "rectangle" "blue" 60 130 25 35) (list "Yvonne" "ellipse" "white" 40 110 50 40) (list "Zed" "rectangle" "grey" 90 60 25 5) )) Analyzing algorithmic efficiency Binary Search: Making searching more efficient using divide-and-conquer Lab