Currently stuck on a problem with binary search that is asking me to pass one parameter with is an object. But is it possible to do this?? Normally I would use two parameters for a problem like this. Normally with binary search I use-->
int binarySearch(int[] list, int searchItem)
{
int mid=0;
int start=0;
int end=list.length-1;
boolean found=false;
//Loop until found or end of list.
while (start <= end && !found)
{
mid = (start + end) / 2;
if (list[mid] == searchItem)
found = true;
else
if (list[mid] > searchItem)
end = mid - 1;
else
start = mid + 1;
}
if(found)
return mid;
else
return(-1);
}
But is it possible to just pass in one parameter like this?? I need to search an array list.
public int binarySearch(Moon searchItem){
int mid = 0;
int start = 0;
int end = moons.size() -1;
boolean found = false;
while(start <= end && !found){
mid = (start + end) / 2;
if(moons.get(mid).equals(searchItem)){
found = true;
}
else{
if(???)) {
}
else
etc etc
}
}
return 0;
}