tribble.util
Class BinarySearch

java.lang.Object
  extended by tribble.util.BinarySearch

public class BinarySearch
extends java.lang.Object

Contains methods for searching arrays.

Since:
2000-04-22
Version:
$Revision: 1.2 $ $Date: 2003/02/10 06:22:00 $
Author:
David R. Tribble, david@tribble.com.
Copyright 2000 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.
See Also:
Comparable

Field Summary
static int SERIES
          Class revision number.
 
Constructor Summary
BinarySearch()
           
 
Method Summary
static int find(java.lang.Object[] tbl, Comparable key)
          Searches array tbl looking for an element of the array that compares equal to key using a binary search algorithm.
static int find(java.lang.Object[] tbl, java.lang.Object key, Comparable comp)
          Searches array tbl looking for an element of the array that compares equal to key using a binary search algorithm.
static void main(java.lang.String[] argv)
          Test driver.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SERIES

public static final int SERIES
Class revision number.

See Also:
Constant Field Values
Constructor Detail

BinarySearch

public BinarySearch()
Method Detail

find

public static int find(java.lang.Object[] tbl,
                       java.lang.Object key,
                       Comparable comp)
Searches array tbl looking for an element of the array that compares equal to key using a binary search algorithm. The key is compared to elements in the array using the comparison method in comp.

This algorithm always finds the first matching element in the array (if there is one) even if there are multiple matches.

The compare() method of comp is always called with key as its first argument, and an element of tbl as its second argument.

Parameters:
tbl - The array to be searched.
key - The key value of the element to locate within the array.
comp - Contains the comparison method with which to compare the key to the array elements during the search.
Returns:
The index of the element if it was found, otherwise -1.
Since:
1.1, 2000-04-22

find

public static int find(java.lang.Object[] tbl,
                       Comparable key)
Searches array tbl looking for an element of the array that compares equal to key using a binary search algorithm.

The key is an object that implements the Comparable interface. Each iteration through the search involves comparing the key to an element in the array, using the key's compareTo() method.

This algorithm always finds the first matching element in the array (if there is one) even if there are multiple matches.

Parameters:
tbl - The array to be searched.
key - The key value of the element to locate within the array.
Returns:
The index of the element if it was found, otherwise -1.
Since:
1.1, 2000-04-22

main

public static void main(java.lang.String[] argv)
Test driver.

Since:
1.1, 2000-04-22