X477: Binary Search 2

Below is an implementation of binary search on a list of objects of type T. In this case, you can assume that T will always extend Comparable and that if you ran x.compareTo(y) and got -1, then x<y.

Fill in the remaining lines to complete the method. You can always assume that T ray[] will be an array sorted from smallest to largest value.

Your Answer:

x
 
1
public int binarySearch2(T ray[], T target, int first, int last) {
2
    if (first > last) {
3
        return -1;
4
    }
5
    else {
6
        int mid = (first + last) / 2;
7
        if (//Add Conditional Here!) {
8
            return mid;
9
        }
10
        if ( //Add Conditional Here!  ) {
11
            return binarySearch2(ray, target, mid + 1, last);
12
        }
13
        else {
14
            //Add return statement here!
15
        }
16
    } // end else
17
}
18
}// end BSH
19
Reset

Practice a different Java exercise

Feedback

Your feedback will appear here when you check your answer.