Week 9: Search Algorithms

Week 9 Goals

  • Practice with lists of objects

  • Search Algorithms (linear and binary)

  • Complexity Analysis of algorithms

  • More print formatting examples

Get Week 9 In-class Code

To copy over the week 9 in-class example programs, do the following (If you have trouble with either of these steps, ask a Ninja or your professor for help):

  1. Create a w09-searching subdirectory in your cs21/inclass directory, and cd into it:

    $ cd ~/cs21/inclass
    $ mkdir w09-searching
    $ cd w09-searching
    $ pwd
    /home/yourusername/cs21/inclass/w09-searching
  2. Copy over the week 9 files into your w09-searching subdirectory (check that they copied successfully copied by running ls:

    $ cp ~admin21/public/w09-searching/* ./
    $ ls
    data.txt formatted_print.py objects_list.py search.py search_worksheet.txt

Week 9 Files

  • formatted_print.py: examples of formatting print output (tabular format)

  • objects_list.py: example of creating and using a list of objects (StudentRecord objects). The StudentRecord class has the following method functionss:

    StudentRecord(name, age, major, gpa): the constructor (creates a new StudentRecord object)
                  name: the studen't name    (string)
                  age: the student's age     (int)
                  major: the student's major (string)
                  gpa: the student's gpa     (float)
    get_age(): returns the age value (int)
    get_gpa(): returns the gpa value (float)
    get_major(): returns the major value (string)
    get_name(): returns the name value (string)
  • data.txt: an input file for the objects_lists.py program

  • search.py: start of a program to implement some searching algorithms

  • search_worksheet.txt: a binary search worksheet. At the top of the file are instructions on how to print this to a CS lab printer.

Searching

Computer Science as a discipline is focused on two primary questions:

  1. What types of problems can be solved computationally?

  2. How efficiently can these problems be solved?

One of the core problems we computers are asked to solved is the search problem. Broadly speaking, the search problem searches for a query item in a (potentially very large) set of potential matches. For two large internet companies, search is one part of their core business model.

Searching efficiently can help to solve larger sized problems, can help to support more customers simultaneously, can help to reduce energy costs associated with running applications, can lead to larger profits, can lead to new new discoveries from Big Data analyses, …​

This week we are going to consider:

  1. How can we search for a value in a list (what are some algorithms for searching, and when can we use different search algorithms)?

  2. How efficient are different search algorithms? And what are some ways in which we can compare algorithms to answer this question?