Quick question here about implementing binary search :-). This is the function I have now, I haven't quite figured out the logic for passing the correct number for i. However, when I debugged this function, I found that when value = values[i] (match), unintended behavior occurred.
My intention was for true to be returned, thus triggering a match and exiting the function. Actual behavior result in return true; and followed by return false; (incorrectly). Does anyone know how to ensure return true; exits the function? Or tips to guide me towards my mistake? Thanks so much!
bool search(int value, int values[], int n){
int i = ((n - 1) / 2);
if(value == values[i])
{
return true;
}
else if(value < values[i])
{
n = i;
search(value, values, n);
}
else if(value > values[i])
{
n = (i * 1.5);
search(value, values, n);
}
return false;
}