CSC151 2007S, Class 45: Binary Search Admin: * EC for ... * Tonight's talk on mental health. * Some Pride Week activity, particularly the Saturday parade. * Thursday's convo. * Thursday's Thursday Extra (Dr. Davis). * Are there questions on the project? * Do we have to squish our squares and circles? * I'd prefer that you do, unless you have good artistic reasons for keeping aspect ratio * What is a nice size for images? * Well, we'll be putting them in digital frames, so 640x480 will probably work or 480x640 * There is no reading for Wednesday. Overview: * The problem of searching. * Analyzing algorithmic efficiency. * Making searching more efficient using divide-and-conquer. * Lab. ==Searching== * Given a collection of values, find one that meets some criterion * How? assoc gives a good model, at least for lists * Either (a) find the value or (b) run off the end * If there are N values in the list, how many values do we expect to look at? * If we only look for things in the list, on average, we look at N/2 values * If we only look for things not in the list, on average, we look at N values ==Improving the Search== * Can we do better than c*N? * Yes, if we organize the data. * Consider phone books, which are organized by name * Jump to "appropriate section" * And then search there * THe computer scientist's favorite technique: Divide-and-conquer * Break in half * SOlve for one or both halves For binary search: * Look in the middle, figure out if the thing you're looking for is in the left half or the right half * Throw away other half, and recurse * Eventually, you find the thing in the middle, or run out of stuff How long does it take to find something in a vector of 1000 values * 1000 => 500 => 250 => 125 => 63 => 32 => 16 => 8 => 4 => 2 => 1 Ten steps to search through a list of 1000 people. Eleven steps to search through a list of 2000 people. Twelve steps to search through a list of 4000 people. Twenty steps to search through a list of 1000000 people. Name: Binary Search Technique: Divide and conquer