Algorithms and OOD (CSC 207 2014F) : EBoards

CSC207.01 2014F, Class 23: Linear and Binary Search


Overview

Preliminaries

Admin

Upcoming Work

Cool Things Coming to Campus

Academic

Peer Support

Miscellaneous

Questions

For the interface, should it use a Web client and prompt for the site?

Yes

Can it just do one iteration through the list?

Yes

Can we return a new UshahidiTestingClient or something similar for extra 2?

Yes

Can we do without the array for extra 2?

Yes

Don't we need clients for extra 2?

Yes

Designing search methods

Analyzing binary search

    binarySearch(val, values, order
       return binarySearch(val, values, 0, values.length, order)

    // binary search in a subarray
    binarySearch(val, values, lb, ub, order)
      if (lb >= ub)
        thrown an exception
      else 
        mid = average(lb,ub)
        if order.compare(values[mid], val) < 0
          return binarySearch(val,values, mid+1, ub, order)
        else if order.compare(values[mid], val) > 0
          return binarySearch(val,values, lb, mid (or mid-1), order)
        else
          return mid

How long does this take?

    time(n) = c + time(n/2)
    time(1) = d // no recursion

Given a recurrence relation that defines a function, you can write the function in "closed form" with not recursion. Read the rest in the reading. We'll do more later.

Testing binary search

Lab

What do you think about using the following method from java.util.Array?

static int[] copyOfRange(int[] original, int from, int to)

How do we test for an exception?

    boolean failed = false;
    try
      {
        CodeWeExpectToFail;
      }
    catch (Exception e)
      {
        failed = true;
      }
    if (!failed)
      fail("Did not throw an exception in ...");