public class LinearSearch extends java.lang.Object implements SearchingAlgorithm
This search method is generally used by other methods, or by sorting
algorithms. It does not assume that the keys are in order. However, it is
very slow for worst-case, so you should pre-sort your array and use
BinarySearch or some other algorithm instead.
This isn't declared as a singleton, since it has no actual overhead (no
state), so there's not strong reason to limit instantiation. However an
standardInstance() method is provided that provides a shared
instance.
You can also just use this via the handy convenience method
SearchingLib.linearSearch(SearchingGopher, Object); that is actually
the recommended method.
Copyright 2004-2006 Partner Software, Inc.
| Constructor and Description |
|---|
LinearSearch()
Creates a new LinearSearch algorithm object.
|
| Modifier and Type | Method and Description |
|---|---|
int |
findClosest(SearchingGopher gopher,
java.lang.Object key)
Finds the slot where the given key would go if it were in there.
|
int |
findClosest(SearchingGopher gopher,
java.lang.Object key,
int start,
int end)
Finds the slot where the given key would go if it were in there
over the given inclusive range (start <= i <= end).
|
int |
search(SearchingGopher gopher,
java.lang.Object key)
Searches for the given key using the given gopher.
|
int |
search(SearchingGopher gopher,
java.lang.Object key,
int start,
int end)
Searches for the given key using the given gopher over the given range
(start <= i < end).
|
static LinearSearch |
standardInstance()
Returns a singleton shared instance.
|
public LinearSearch()
standardInstance() method.public static LinearSearch standardInstance()
public final int search(SearchingGopher gopher, java.lang.Object key)
SearchingAlgorithmsearch in interface SearchingAlgorithmgopher - adapter to underlying array-like structurekey - value to search forpublic final int search(SearchingGopher gopher, java.lang.Object key, int start, int end)
SearchingAlgorithmsearch in interface SearchingAlgorithmgopher - adapter to underlying array-like structurekey - value to search forstart - start of index range, inclusive (start <= i)end - end of index range, exclusive (i < end)public final int findClosest(SearchingGopher gopher, java.lang.Object key)
SearchingAlgorithmfindClosest in interface SearchingAlgorithmgopher - adapter to underlying array-like structurekey - value to search forpublic final int findClosest(SearchingGopher gopher, java.lang.Object key, int start, int end)
SearchingAlgorithmfindClosest in interface SearchingAlgorithmgopher - adapter to underlying array-like structurekey - value to search forstart - start of index range, inclusive (start <= i)end - end of index range, exclusive (i < end)