CSC 151.03, Class 35: Binary search
Overview
- Preliminaries
- Notes and news
- Upcoming work
- Extra credit
- Questions
- Divide and conquer
- Destructive binary search (a demonstration)
- Binary search in Scheme
- Lab
- Debrief
News / Etc.
- Quiz 12 returned.
- I expect that the next set of grades you get from me will be after Thanksgiving.
Upcoming Work
- Writeup for class 35 due Wednesday at 10:30 p.m.
- Exercise 1b and self-check 3i
- To: csc151-03-grader@grinnell.edu
- Subject: CSC 151.03 Writeup 35 (YOUR NAMES)
- Project proposals due TONIGHT.
- Projects due next Tuesday.
- No reading for Wednesday.
- No quiz this week.
- No mentor sessions.
Extra credit (Academic/Artistic)
- Tuesday community hour panel on guns.
- CS table tomorrow: Uber and “the gig economy”.
Extra credit (Peer)
Extra credit (Misc)
Other good things
- Harp ensemble Tuesday night.
- Questbridge potluck during Turkey break.
Questions
Divide and conquer
We’ve seen one way of searching lists: Look at each element in turn. Look at element 0. If it matches, you’re done. Otherwise, go on to the next element. Look at element 1. If it matches, you’re done. Otherwise, go on to the next element. Look at element 2. If it matches, you’re done. Otherwise, go on to the next element. Look at element 3. If it matches, you’re done. Otherwise, go on to the next element. Look at element 4. If it matches, you’re done. Otherwise, go on to the next element. Look at element 5. If it matches, you’re done. Otherwise, go on to the next element. And so on and so forth.
If you have a list of n elements, you potentially look at every element.
Ask yourself: Can I do better? (Perhaps if I restrict the input?)a
- If we limit ourselves to vectors (fast access)a
- And we require the vector to be ordered from least to greatest
- Then we have a faster way to search.
Algorithm
- Look in the middle of the portion of the vector of interest
- If the thing in the middle matches, we’re done
- If the thing in the middle follows what we’re looking for, throw away everything less than the middle
- If the thing in the middle precedes what we’re looking for, throw away everything greater than the middle
Destructive binary search (a demonstration)
How many steps did “destructive binary search” take?
- 1600 -> 800 -> 400 -> 200 -> 100 -> 50 -> 25 -> 12 -> 6 -> 3 -> 1 -> DONE
- Eleven steps! Much better than 1600
How many steps does it take to find an Oklahoman (state has approximately 4,000,000 people). Only 22 steps (plus or minus)
Binary search in Scheme
Explain each of the four parameters to binary search.
(define binary-search
(lambda (vec get-key may-precede? key)
vecis a vector of lists of scheme values, each of which represtents one “thing” (element)- More generally, the elements of the vector can be anything, as long as we can use them to represent some conceptual thing
- Ordered by key
keyis a value that represents what we’re looking forget-keyis a procedure, like cadr, that extracts a value from an element in the vector; we will compare that value to the key. We think of the part of the element as “the key of that element”may-precede?is a compartor that we use to find out if you should keep looking and, more generally, which section.
Examples of get-key
(define courses
(vector
(list "CSC-151-01" "Functional Problem Solving" "Rebelsky" 36)
(list "CSC-151-02" "Functional Problem Solving" "Klinge" 33)
(list "CSC-161-01" "Imperative Problem Solving" "Weinman" 28)))
- If this is organized by course number, get-key is
car. If it’s organized by enrollment, get-key is(r-s list-ref 3)(or something like that).
(define administrators
(vector
(list "Kington" "Raynard" "President")
(list "Latham" "Michael" "Dean")
(list "Smith" "Jane" "VP for One thing")
(List "Smith" "Jessica" "VP for Somethiung else")))
Here, I might think of a key as “Last name plus first name”. So
get-key is something like (lambda (entry) (string-append (car entry) " " (cadr entry)))
How do we apply the “divide and conquer” technique without building new vectors (which is computationally expensive)?
- We don’t actually “throw away” elements. Instead … keep track of range of interest.
Lab
- Why does
binary-searchwork withstring-ci<=?and notstring-ci<?? - Think about how
binary-searchdecides if we’ve found the matching key. - How do I get element 4 of a list?
- There’s a hint up above.
- What’s the writeup?
- Exercise 1b plus self-check 3i
- Make sure to report the number of recursive calls!
- Why did I get the middle Otto and the last Amy?
- Explain to your mentor
Debrief
There was no debrief so that students had five extra minutes to finish the lab writeup.