Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ABAGAIL.arrays fails search when sample is not found #68

Open
jakeman792 opened this issue Apr 18, 2018 · 1 comment
Open

ABAGAIL.arrays fails search when sample is not found #68

jakeman792 opened this issue Apr 18, 2018 · 1 comment

Comments

@jakeman792
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant