I have a binary search code for an object array and I cant f
I have a binary search code for an object array and I can\'t figure out why it\'s not working. Just searching for a Student Obj name. Any help would be greatly appreciated. Thanks!
I have this code
public static int findStudent(Student[] s, String name) {
int first = 0; int last = s.length - 1;
while(first <= last) {
int mid = (first + last) / 2;
if(s[mid].equals(name)) return mid;
else if(s[mid].compareTo(name) < 0)
return findStudent(s, name);
}
return findStudent(s, name); }
{
Solution
public static int findStudent(Student[] s, String name) {
int first = 0; int last = s.length - 1;
int res = -1,mid;
while(first <= last) {
mid = (first + last) / 2;
if(s[mid].equals(name))
{
res = mid;
break;
}
else if(s[mid].compareTo(name) < 0)
first = mid+1;
else
last = mid-1;
}
return res; }
