Lecture 16:  Searching

Announcements

·       H6 is posted, due 3/17

·       P3 is due 3/15

 

Searching

·       an important problem

·       given a collection of values,

     determine whether a given value, v is there

a common variation: 

given a collection of unique keys and some associated info, use a given key value v to fetch the associated info

examples

  dictionary:  key = word,  associated info = definition

  phone book:  key = name, associated info = phone #, address

Can store values in:

·       an array

·       another data structure – binary search tree, hash table…

Searching in an array

problem:  given an array of N values

                determine whether v is there

two approaches:

        sequential search

        binary search

 

Sequential search

·       look at each value in turn (A[0], A[1], A[2]…)

·       quit when v found, or all values looked at

worst case time:  O(N)

Note: if array is sorted,

·       look at each value in turn

·       quit when v found or current value > v or all values looked at

worst case time is still O(N)

Binary search

given:  array A of size N, must be sorted

    initially:  consider whole array             (lo = 0, hi = N)

·       look at value in middle position k          k = (hi – lo)/2 + lo

·       if A[k] = v quit; otherwise eliminate ½ of A & repeat

if A[k] < v, eliminate left half;         new lo = k + 1; 

if A[k] > v,  eliminate right half;      new hi = k – 1;

·       quit when A[k] = v or entire array is eliminated    hi < lo

worst case time = # of times that N can be divided in half

        = O(log N)

 

Play 20 questions

        1/3 of class pairs up, each pair thinks of an animal

        each pair splits up, works with 2 students in other group

                one group of 3:  quess using normal 20 questions

                other group of 3:  use binary search (any words)

                record total # guesses for each algorithm & animal

Implementing Binary Search in Java

·       array must be sorted

·       for values x and v, must be able to ask if x < v

o      ok for primitive numeric types (int, float, double)

o      what about Objects ??

Objects for which "is less than" makes sense

·       have a compareTo method:  

int compareTo(Object ob) { … }

·       implement the Comparable interface

Example

String s1, s2;

…

if ( s1.compareTo(s2) ) …

        returns

o      a negative number if s1 is less than s2

o      zero if s1 is equal to s2

o      a positive number if s1 is greater than s2

when you define a class

        if "is less than" makes sense for two instances,

then you should

o      declare that your class  implements Comparable

o      define a compareTo method

You try:

define a Fraction class with 2 fields:

        int numerator

        int denominator

and a compareTo method     [write this method]

·       assume that a negative fraction has a negative numerator

·       assume the denominator is always positive

(There are several possible ways to write compareTo)

 

One solution:

public int compareTo (Object ob) {

  // exception if ob is not a Fraction

  long prod1 = numerator *((Fraction)ob).denominator;

  long prod2 = ((Fraction)ob).numerator*denominator;

  if (prod1 < prod2) return -1;

  if (prod1 == prod2) return 0;

  return 1;

}

 

another solution:

    convert to double and compute the fractions

    – round-off error?