ABAGAIL
ABAGAIL copied to clipboard
ABAGAIL.arrays fails search when sample is not found
The search method in ABAGAIL.arrays does not return a valid index when the value found is at the top end of the distribution. The start 'high' value is the length of the array instead of length - 1, which results in the 'high' value sometimes returning an index that is out of bounds from the array.
here is a solution
public static int search(double[] a, double v) {
int high = a.length-1;
int low = -1;
while (high - low > 1) {
int mid = (high + low) / 2;
if (a[mid] < v) {
low = mid;
} else {
high = mid;
}
mid = low + (high - low) / 2;
}
return high;
}