I am implementing recursive Binary Search in Java using ArrayList. Here is my code:
public static boolean binarySearchRecursive(List<Integer> list, int item) {
int midpoint = (list.size()) / 2;
System.out.println(list.get(midpoint));
if (list.get(midpoint) == item) {
return true;
} else if (list.get(midpoint) > item) {
binarySearchRecursive(list.subList(0, midpoint), item);
} else if (list.get(midpoint) < item) {
binarySearchRecursive(list.subList(midpoint + 1, list.size()), item);
}
return false;
}
Then I call binarySearchRecursive providing arguments: ArrayList with size = 1 000 000, where index == value; and item == 64327.
Console output is:
... 64241, 64317, 64355, 64336, 64327
So I see midpoint got value of item and method should return true after comparation, but method constantly return false. What is pecularity of this method? Why code proceeds until last return statment, and return false from there instead of return true from 5th line?