Skip to main content

CSC 151.01, Class 48: Binary Search

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Extra credit
    • Questions
    • Friday PSA
  • Quiz
  • Analyzing algorithm efficiency
  • The problem of searching
  • Divide and conquer
  • Destructive binary search (a demonstration)
  • Design issues

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.
    • NEW 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!
  • NEW 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)
  • Gardner Show 8pm Monday. Japanese Breakfast (like the Eagles, but hip)
  • Singers/Symphony/Oratorio concert Sunday at 2pm in Chapel. Epic Sounds!

Extra credit (Misc)

  • Make Your Mark on Phase 1!, Tuesday, May 2, 10 a.m.-2 p.m. (I think), 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)
  • Zombies vs. non-Zombies (also ? whether or not it’s a good thing to do)
  • See the solar eclipse in August (go to Missouri or SW Iowa), Monday of New Student Days.

Questions

Friday PSA

  • Please do not feel pressure to consume or to cohabit. Lots of people don’t.
  • Avoid pressure by planning in advance.
  • If you consume, consume in moderation.
  • If you cohabit, CONSENT IS ESSENTIAL.
  • Treat yourself and your peers respect. You and they deserve it.

Quiz

Analyzing algorithm efficiency

  • When designing algorithms, and particularly when comparing algorithms, we like to know how “fast” they are. We often measure this by counting procedure calls.
    • We can also use wall-clock time
  • Given a choice, we will choose the one that seems “faster”.
  • We generally pay attention primarily to the function of the number of calls to the “input size”
  • Some procedures always take the same amount of time, no matter what the input is.
    • car takes one step on a list of length 1 or 1000 or 1000000000000. That’s a “constant time” algorithm.
    • Some algorithms have a constant number of procedure calls per element in the input. length is linear.
    • Some algorithms take approximately size*size steps. Quadratic
    • A few algorithms take 2^size steps. Exponential.
  • When you see an algorithm ask “Can we do better?”
    • Variant: “If we place restrictions on the input, can we do better?”

The problem of searching

  • Given a collection of values
  • Given a desired “key” of a value
  • Find the value in the collection with that key

Linear search (association lists)

  • Given a key and a list of keyed values
  • Start at the front of the list
    • Compare the key to the key of the value
    • If they are the same, return the value
    • If they are different, do it again
    • Stop when you hit the end of the list$a

If we did this with a vector, instead of a list, how would it differ?

  • We’d use vector-ref instead of car and cdr.
  • We’d have a counter to keep track of where we are in the vector.

Divide and conquer

Given the student db, how should we look for a person?

Observation: This is not the general case. The db is organized alphabetically by name.

Option 1: Linear search. If there are n students, this will take us at most n comparisons. Linear.

Option 2: Improved linear search: Use the index to get to the right starting letter of the last name. Search only that letter. If there are n students, this will take us n/?. Still linear.

Option 3:

Look in the middle
   If you find it.  Done
   If the middle comes after the person you are looking for, look in
     the first half
   If the middle comes before the person you are looking for, look
     in the second half
If you run out of elements, the person is not there.

Destructive binary search (a demonstration)

Finding a student in the directory.

Each time, we cut the search space in half.

1500 -> 750 -> 375 -> 190 -> 95 -> 47 -> 23 -> 11 -> 5 -> 2 -> 1 -> 10

The manhattan phone book has approimately 4 million people. How many steps?

Approximately “The number of times you divide 4 million by 2 in order to get 1.”

2^k = 4 million

log_2(4 million) ~ 22

Binary search is a really nice technique.

Design issues

Can you binary search a list? No. It takes too long to get to the middle. We assume getting to the middle is one step, but in a list it’s about n/2 steps.

Can you binary search a vector? length/2 is the middle

How do you look in half? Keep track of the boundaries of the portion that is still of interest.

Read more this weekend.