OneBug This week's bugs
Spot the bug Java
1
// Index of key in a sorted array, or -1.
2
static int indexOf(int[] a, int key) {
3
    int low = 0, high = a.length - 1;
4
    while (low <= high) {
5
        int mid = (low + high) / 2;
6
        if      (a[mid] < key) low  = mid + 1;
7
        else if (a[mid] > key) high = mid - 1;
8
        else                   return mid;
9
    }
10
    return -1;
11
}

Read the full write-up for this bug (spoilers)