I m 试图建立一个用扼杀而不是ger器的宾平搜索方案。 问题在于,我不知道如何使数量少而少,这样就显得微不足道。
例如
阵列的阵列少于扼杀值。
/**
The StringBinarySearcher class provides a public static
method for performing a binary search on an String array.
*/
public class StringBinarySearcher
{
/**
The search method performs a binary search on an String
array. The array is searched for the number passed to
value. If the number is found, its array subscript is
returned. Otherwise, -1 is returned indicating the
value was not found in the array.
@param numbers The array to search.
@param value The value to search for.
*/
public static int search(String[] numbers, String value)
{
int first; // First array element
int last; // Last array element
int middle; // Mid point of search
int position; // Position of search value
boolean found; // Flag
// Set the inital values.
first = 0;
last = numbers.length - 1;
position = -1;
found = false;
// Search for the value.
while (!found && first <= last)
{
// Calculate mid point
middle = (first + last) / 2;
// If value is found at midpoint...
if (numbers[middle] == value)
{
found = true;
position = middle;
}
// else if value is in lower half...
// needs array to be less then the string value?, without using equality regulators
else if (numbers[middle].compareTo(numbers[middle +1]) > 0)
last = middle - 1;
// else if value is in upper half....
else
first = middle + 1;
}
// Return the position of the item, or -1
// if it was not found.
return position;
}
}