Skip to main content

Class 48: Binary Search

Held:

We consider the general problem of searching. We explore *binary search, one of the most efficient algorithms for searching.*

Preliminaries

Overview

  • Analyzing algorithm efficiency
  • The problem of searching
  • Divide and conquer
  • Destructive binary search (a demonstration)
  • Design issues

Updates

News / Etc.

  • Keep your partners.
  • Grading update
    • “The best laid schemes of mice and men gang aft aglay.”
    • Everything should be returned by Monday.

Upcoming Work

  • NO lab writeup
  • Reading for Monday: Search algorithms
  • Projects due Tuesday.
  • Exam 4 distributed Wednesday.

Extra credit (Academic/Artistic)

  • The Harold W. ’38 and Jean Ryan ’38 Squire Lectureship in Physics. May 2nd, 7:00 p.m., ARH 302. Astrophysicist Kartik Sheth (Grinnell class of 1993) will present From Mumbai to Grinnell to NASA HQ - Adventures in Time and Space
  • CS Extras, Thursday, May 4, 4:15 p.m.: Inclusion in CS. Please attend.

Extra credit (Peer)

  • LiNK Fundraiser: They are selling kimbab from 1-4pm ($2 vegan, $3 meat or seafood, $4 everything) to support North Korean refugees in China resettle in South Korea and the US.
    • Yes, I will reimburse you if the cost is a burden.
  • Dance Ensemble show, Staying with the Trouble. Tickets are available in Bucksbaum box office for Thu, Fri, Sat at 7:30 and Sunday at 2:00.
  • Drill Team Show, Friday, May 5, at Triple V stables. The other side of the golf course. Free rides (cars, not pony) to the event!
  • GSEA (Grinnell Space Exploration Agency) is launching a weather balloon on May 7th at 5:30AM. The balloon will go to almost-space and take pictures. It’ll also tell us about its location and the temperature of the air around it as it rises. We’re inviting people to come see the launch, if they want to come (despite the fact that it’s at 5:30AM).
  • Next home baseball game: Sunday, May 7 (Senior Day)

Extra credit (Misc)

  • Make Your Mark on Phase 1!, Tuesday, May 2, 10 a.m., Near the Commencement Stage. (Light refreshments provided.) Leave a hand-print on the construction wall (which will become a Commencement backdrop!) to celebrate the Class of 2017. You can also make chopsticks from wood harvested at the construction site and get your photo taken at the construction-themed photo booth.
  • May 4 town hall on belonging. 11am, May 4, JRC 101

Other good things to do

  • Dag Field Day, Saturday, April 29, noon-5pm, in the Club Athletic Field.
    • Near the Softball diamond.
  • Raging Cow Atlatl event, Saturday, Club Athletic Field, 9:00 a.m.
  • Titular head (? whether or not it’s a good thing to do)
  • HvZ (also ? whether or not it’s a good thing to do)

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.
  • We’ll do a quick demonstration of binary search

Dsigning a 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

  • We will do the the lab next week.