public static boolean binarySearch(ArrayList<Student> students, int search) {
int first = 0;
int last = students.size() - 1;
int mid;
while (first <= last) {
mid = first + (last - first) / 2;
if (search == students.getTotal(i)) {
return true;
} else if (students.compareTo(students.get(mid)) < 0) {
last = mid - 1;
} else {
first = mid + 1;
}
}
return false;
}
In this method I am supposed to return true
if search
is found, and false
if search
is not found.
However I am unable to retrieve information from the ArrayList
.
There are two errors here.
One at
search == students.getTotal(i)
Another at
students.compareTo(students.get(mid))
For the first one I am supposed to access students
and compare the i
to search but I am unable to retrieve the data in students
.
In the second one I am supposed to get if the search were to continue in the LEFT or RIGHT side.
Can anyone give me any tips on how to solve this? I might know that I am using the wrong way to get data but I need a direction to go to.