Outline 46: Binary Search
Held: Monday, 24 November 2014
Back to Outline 45 - Pause for Breath.
On to Outline 47 - 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
- New partners!
- My computer is in the shop, which puts me even further behind. I probably
won't get you comments on phase 1 of the project, which means that I'll be
a little more generous than normal in grading phase 2. Apologies.
Upcoming Work
- No reading for tomorrow.
- Part two of the project due tomorrow.
Extra Credit Opportunities
Academic
- TEC Extras tommorrow at 4:15 in Burling: Emma Lange on UI/UX on
the Libraries' Web Sites
- CS Extras Thursday the 4th: Summer Opportunities in CS
Peer Support
- Karan's radio show 11pm Thursday nights on KDIC
- Evan's radio show 5pm Friday nights on KDIC
- Donna's radio show Sunday midnight on KDIC
- Noteworthy, coming soon
- One acts, coming soon
Project Questions
We're drawing/selecting polygons with turtles. We realize that in order
to get it to scale we would have to change the angels of our polygons
so that they would no longer follow the (/ 360 sides) formula. What
should we do?
Note that you would also have to change the length of each side. But
both figuring out the angles and figuring out the side length is not
reasonable. Hence, I am comfortable as long as your scaling works
within the same aspect ratio, even if it does not work with different
aspect ratios. (E.g., if the image goes from 100x100 to 200x200 or
from 100x200 to 200x400, the turtle portions should scale correctly,
but if it goes from 100x100 to 200x100 or 100x200, then the turtle
portions do not need to scale correctly.) I'd recommend that you
base your side length on either the minimum, maximum, or average of
the dimensions of the image.
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.
- Sometimes, the collection is sorted. 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