Outline 45: Binary Search
Held: Tuesday, 21 April 2015
Back to Outline 44 - Association Lists.
On to Outline 46 - Binary Search Lab.
Summary
We consider the general problem of searching and binary search,
one of the most efficient algorithms for searching.
Related Pages
Overview
- The problem of searching.
- Analyzing algorithmic efficiency.
- Making searching more efficient using divide-and-conquer.
- A demo: Destructive binary search.
- Considering the parameters to binary search.
Administrivia
- Continue partners!
- Review sessions Thursday.
- For office hours this week, check http://rebelsky.youcanbook.me
- 5-10 min admin, 5-10 min project, 15-20 min sorting, remainder lab.
- When you send me email questions of the form "this doesn't work",
- Send me all the code I will need in order to run examples, including
any utilities you are using.
- Send me the expression that you are using that's not working.
- Explain to me what's happening.
Upcoming Work
Extra Credit Opportunities
Academic
- Convocation April 22nd, Erica Lehrer '92 (Post-Conflict Memory, Ethnography)
- CS Talks Wednesday:
Peer Support (Morning Section)
- KY's radio show, "We Think We're Funny", 9-10pm Mondays.
- Julia's radio show, "The Hot Box". Wednesday night/Thursday
morning 1:00-2:00 a.m.
Peer Support (Afternoon Section)
- Contra Dance April 24 in Main Hall.
- Financial Literacy workshop Monday night 7-8 in JRC 226.
Miscellaneous
- Town hall April 23, noon or 7:30 pm, "How we have conversations."
Other Good Things (no extra credit)
- Contra Dance April 24 in Main Hall.
Project Tips
Image-making techniques
- Drawings as values, with or without recursion
image-variant, `image-transform!
- Also
(image-redo! image (lambda (col row color) ...)
image-transform! and image-redo! behave differently with
selections.
image-compute
- Gimp tools, with or without recursion
- Turtle graphics
I've added a variety of project tips that I thought we'd look at
quickly.
- Recoloring a turtle's drawing. (Needs improvement.)
- Selecting a polygon.
You can also find a few others on the tips page, such as a procedure
to copy and paste.
Common Problems and Algorithms
- As we discussed early in the semester, a key aspect of computer
science is the design of algorithms, formalized processes
that provide solutions to problems.
- There are a number of common problems for which computer scientists
have developed common solutions.
- We'll visit two problems over the next few days: searching and
sorting.
- As we develop algorithms, we'll consider intuitive ways that one
might come up with the algorithms.
Searching
- Goal: Find a value in a collection.
- Typically, the collection is linear: A vector or list.
- Sometimes, the collection is also unordered. That is, there is no
known arrangement to the list. For example, the books on the MathLan
book shelves are not in an arrangement that would make it easy to
search for a book with a particular title or by a particular author.
- For unordered collections, the typical search is sequential::N
search, look at each element in turn.
- If we distribute things between multiple computers, we can spend
the same total computing effort, but significantly less time.
- Sometimes, the collection is ordered. That is, the collection
is organized by the primary key in which we search.
- For example, a phone book is sorted by name.
- However, we can also use something known as binary search:
- Look in the middle of the collection.
- If the middle is too small, anything smaller is also too small,
so discard and try again.
- If the middle is too large, anything larger is also too large,
so discard and try again.
- If the middle is just right, you're done.
Demonstration: Destructive Binary Search
- We'll do a quick demonstration of binary search
Exploring the Search API
Suppose we have data for various students on campus: Last Name (string),
First Name (string), Graduation Year (integer), Box Number (integer),
and Phone number (string). We might search by any of the four criteria
(and by other criteria) and we might therefore order in various ways.
(define people
(vector
("Aanderson" "Aan" 2017 4114 "x4410")
("Brown" "Bruin" 2016 8123 "x9000")
("Doe" "J" 2018 9999 "none")
("Smith" "Kieran" 2015 4112 "x9231")
("Taylor" "Mic" 2017 1234 "x0001")))
- Right now, we have things sorted by last name (and, surprisingly, by
first name), so we might search by last name (or by first name).
- If we wanted to search by year, we'd order by year (although that's
not likely to give particularly useful results).
- If we wanted to search by box number, we'd order by box number.
- Our search algorithm needs to know how to find the key in an entry
- Last name: car
- First name: cadr
- Year: caddr
- Box number: cadddr
- Our search algorithm needs to know how the keys are ordered
- Traditionally, strings are ordered alphabetically
- Traditionally, numbers are ordered numerically, from smallest to largest
- So our binary search algorithm needs four parameters:
- The vector to search
- The key to search for
- The instructions for getting a key from each element
- The mechanism used to order the elements
- All of this is a bit more complicated if we have a compound key,
such as "last name plus first name plus phone number"
Lab