public static int binSearch (double[] list, double item)
{
int bottom = 0;
int top = list.length - 1;
int middle = 0;
boolean found = false;
int location = -1;
while (bottom <= top && !found)
{
middle = (bottom + top) / 2;
if (list [middle] == item)
{
// Success
found = true;
location = middle;
}
else if (list [middle] > item)
// Item cannot be in bottom half
bottom = middle + 1;
else
// Item cannot be in top half
top = middle - 1;
}
return location;
}
|