ABAGAIL icon indicating copy to clipboard operation
ABAGAIL copied to clipboard

ABAGAIL.arrays fails search when sample is not found

Open jakeman792 opened this issue 7 years ago • 1 comments

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.

jakeman792 avatar Apr 18 '18 12:04 jakeman792

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;
}

jakeman792 avatar Apr 18 '18 12:04 jakeman792