drasys.or.alg
Class BinarySearch

java.lang.Object
  |
  +--drasys.or.alg.BinarySearch

public class BinarySearch
extends java.lang.Object
implements SearchI, java.io.Serializable

An implementation of a binary search. The compare object must be set to search for Objects.

References:

Introduction to Algorithms
    Thomas H. Cormen, et al / Hardcover / Published 1990

See Also:
Serialized Form

Constructor Summary
BinarySearch()
          The compare object is not set.
BinarySearch(boolean descending)
          If descending is true then the elements must be in high-to-low order.
BinarySearch(boolean descending, CompareI compare)
          If descending is true then the elements must be in high-to-low order.
BinarySearch(CompareI compare)
          The compare object is set to 'compare'.
 
Method Summary
 int find(byte[] array, byte key)
          Returns the index of a matching element in the block.
 int find(byte[] array, byte key, int from, int to)
          Returns the index of a matching element in the block.
 int find(double[] array, double key)
          Returns the index of a matching element in the block.
 int find(double[] array, double key, int from, int to)
          Returns the index of a matching element in the block.
 int find(float[] array, float key)
          Returns the index of a matching element in the block.
 int find(float[] array, float key, int from, int to)
          Returns the index of a matching element in the block.
 int find(int[] array, int key)
          Returns the index of a matching element in the block.
 int find(int[] array, int key, int from, int to)
          Returns the index of a matching element in the block.
 int find(long[] array, long key)
          Returns the index of a matching element in the block.
 int find(long[] array, long key, int from, int to)
          Returns the index of a matching element in the block.
 int find(java.lang.Object[] array, java.lang.Object key)
          Returns the index of a matching element in the block.
 int find(java.lang.Object[] array, java.lang.Object key, int from, int to)
          Returns the index of a matching element in the block.
 int find(short[] array, short key)
          Returns the index of a matching element in the block.
 int find(short[] array, short key, int from, int to)
          Returns the index of a matching element in the block.
 int find(java.lang.String[] array, java.lang.String key)
          Returns the index of a matching element in the block.
 int find(java.lang.String[] array, java.lang.String key, int from, int to)
          Returns the index of a matching element in the block.
 int find(VectorI vector, double key)
          Returns the index of a matching element in the block.
 int find(VectorI vector, double key, int from, int to)
          Returns the index of a matching element in the block.
 int find(java.util.Vector vector, java.lang.Object key)
          Returns the index of a matching element in the block.
 int find(java.util.Vector vector, java.lang.Object key, int from, int to)
          Returns the index of a matching element in the block.
 CompareI getCompare()
          Gets the compare object.
 int insertionIndex()
          If a find fails, this returns the index where the new element should be inserted.
 void setAscending()
          Sets order to ascending.
 void setCompare(CompareI compare)
          Sets the compare object.
 void setDescending()
          Sets order to descending.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinarySearch

public BinarySearch()
The compare object is not set. The searched elements must be in low-to-high order.

BinarySearch

public BinarySearch(boolean descending)
If descending is true then the elements must be in high-to-low order. The compare object is not set.

BinarySearch

public BinarySearch(CompareI compare)
The compare object is set to 'compare'. The searched elements must be in low-to-high order.

BinarySearch

public BinarySearch(boolean descending,
                    CompareI compare)
If descending is true then the elements must be in high-to-low order. The compare object is set to 'compare'.
Method Detail

setCompare

public void setCompare(CompareI compare)
Sets the compare object.
Specified by:
setCompare in interface SearchI

getCompare

public CompareI getCompare()
Gets the compare object.
Specified by:
getCompare in interface SearchI

setDescending

public void setDescending()
Sets order to descending.

setAscending

public void setAscending()
Sets order to ascending.

insertionIndex

public int insertionIndex()
If a find fails, this returns the index where the new element should be inserted.
Specified by:
insertionIndex in interface SearchI

find

public int find(java.lang.Object[] array,
                java.lang.Object key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(java.lang.Object[] array,
                java.lang.Object key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(java.util.Vector vector,
                java.lang.Object key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(java.util.Vector vector,
                java.lang.Object key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(int[] array,
                int key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(int[] array,
                int key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(short[] array,
                short key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(short[] array,
                short key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(byte[] array,
                byte key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(byte[] array,
                byte key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(double[] array,
                double key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(double[] array,
                double key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(float[] array,
                float key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(float[] array,
                float key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(long[] array,
                long key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(long[] array,
                long key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(java.lang.String[] array,
                java.lang.String key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(java.lang.String[] array,
                java.lang.String key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(VectorI vector,
                double key)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.

find

public int find(VectorI vector,
                double key,
                int from,
                int to)
Returns the index of a matching element in the block.
Specified by:
find in interface SearchI
Returns:
-1 if the element is not found.


Copyright(C)1997-2000 by DRA Systems all rights reserved. OpsResearch.com