You need to update the left
and right
values not the middle
..change it like this
while (true) {
middle = (left + right) / 2;
int copmarison = Name.compareTo(StudentName[middle]);
if (Name.equals(StudentName[middle])) {
return middle;
} else if (left > right) {
return count;
} else {
if (copmarison > 0) {
left = middle + 1;
} else {
right = middle - 1;
}
}
}
1
solved Searching using BinarySearch [closed]