[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Project Discussion. On to Sorting Algorithms.
Held Wednesday, October 6, 1999
Overview
Today, we'll consider the joys of binary search, an important divide and conquer algorithm. We will also consider some underlying object-oriented design issues for comparing objects.
Notes
Contents
Handouts
Summary
boolean binarySearch(Element element, Collection stuff)
If stuff is empty then return false
Identify mid, the middle element of stuff
If element equals mid then
Return true
Else if element < mid then
Let smaller be the elements of stuff smaller than mid
Return binarySearch(element, smaller)
Else // element > mid
Let bigger be the elements of stuff larger than mid
Return binarySearch(element, bigger)
boolean binarySearch(Element element, Collection stuff)
While there are usable elements in stuff
Identify mid, the middle usable element
If element equals mid then
Return true
Else if element < mid then
Mark all elements greater than mid as unusable
Else
Mark all elements less than mid as unusable
int binarySearch(Element element, Element[] stuff)
return binarySearch(element, stuff, 0, stuff.length-1)
int binarySearch(Element element, Element[] stuff, int lb, int ub)
If stuff is empty then return false
mid = stuff[(lb+ub)/2];
If element equals mid then
Return (lb+ub)/2];
Else if element < mid then
Return binarySearch(element, stuff, lb, (lb+ub)/2 - 1);
Else
Return binarySearch(element, stuff, (lb+ub)/2 + 1, ub);
Comparables
and the second Comparators.
Comparable objects.
import IncomparableException;
/**
* Objects that can be compared to each other. Particularly
* useful for objects that must be sorted or searched. Assumes
* that comparison is transitive and that if a < b then b > a.
*
* @author Samuel A. Rebelsky
* @version 1.0 of October 1999
*/
public interface Comparable {
/**
* Determine if this object should precede another object.
*
* @exception IncomparableException
* If such comparison is not possible, such as when you
* try to compare apples to oranges.
*/
public boolean lessThan(Object other)
throws IncomparableException;
/**
* Determine if this object is the same as another object.
*
* @exception IncomparableException
* If such comparison is not possible, such as when you
* try to compare oranges to apples.
*/
public boolean equals(Object other)
throws IncomparableException;
} // interface Comparable
Comparator objects.
/**
* Objects that can compare pairs of objects (for equality and
* ordering). We assume that if something is neither less than
* or equal to another object, then it is greater than that other
* object. Typically used to help with sorting.
*
* @author Samuel A. Rebelsky
* @version 1.1 of October 1999
*/
public interface Comparator {
/**
* Determine if the first element is strictly smaller than the
* second. Throws an exception if the two elements cannot
* be compared.
*/
public boolean lessThan(Object first, Object second)
throws IncomparableException;
/**
* Determine if the first element is equal to the second. Throws
* an exception if the two elements cannot be compared.
*/
public boolean equals(Object first, Object second)
throws IncomparableException;
} // interface Comparator
Comparator.
import Comparator;
/**
* Compares two objects by considering their string representations.
* Does case-insensitive comparision. Particularly useful for
* comparing strings, but can also be used for other objects.
*
* @author Samuel A. Rebelsky
* @version 1.1 of October 1999
*/
public class StringComparator
implements Comparator
{
/**
* Determines if the string reperesentation of the first object
* is the same as the string representation of the second object.
* Pre: The two objects are initialized and can be converted to strings.
* Post: Returns true if they are equal and false otherwise.
*/
public boolean equals(Object first, Object second)
{
String firstString = first.toString().toLowerCase();
String secondString = second.toString().toLowerCase();
return firstString.equals(secondString);
} // equals(Object,Object)
/* Determines if the string representation of the first object
* is less than the string representation of the second object.
* One string is "less than" another iff letters 0 through i-1
* of the two strings are the same and either (i) letter i of the
* first string is less than letter i of the second string or (ii)
* the first string has only i letters and the second string has more
* than i letters.
*/
public boolean lessThan(Object first, Object second)
{
String firstString = first.toString().toLowerCase();
String secondString = second.toString().toLowerCase();
// The compareTo operator returns a negative value if
// the first string precedes the second.
return firstString.compareTo(secondString) < 0;
} // lessThan(Object,Object)
} // class StringComparator
Tuesday, 10 August 1999
Friday, 1 October 1999
Tuesday, 5 October 1999
Comparables and
Comparators.
Wednesday, 6 October 1999
Back to Project Discussion. On to Sorting Algorithms.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.24.html
Source text last modified Wed Oct 6 09:18:03 1999.
This page generated on Wed Oct 6 09:19:02 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu