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;
}
You are missing couple of return
keywords.
search(value, values, n);
should be:
return search(value, values, n);
There are two such lines.
Without those return
keywords, the function always goes to the end and returns false
.
Update, in response to OP's comment
Your function does not have any checks to ensure that values
is not accessed out of bounds. I think it will never try to access anything with a negative index but it has the chance to access elements with an index that is higher than the allowed index values. I suggest changing the function to use:
int search(int value, int values[], int start, int end)
{
if ( start >= end )
{
return 0;
}
int mid = (start + end)/2;
if(value < values[mid])
{
return search(value, values, start, mid);
}
else if(value > values[mid])
{
return search(value, values, mid+1, end);
}
else
{
return (value == values[mid]);
}
}
and call it with right start
and end
values when calling from outside the function.
int values[] = {1, 4, 6, 8};
bool found = search(2, values, 0, sizeof(values));
Let me comment that it is possible to write an effective version of your function with exactly your interface. Something like:
bool search(int value, int values[], int n)
{
if (n == 0) // searching in an empty set?
return false;
int i = (n - 1) / 2;
if (value == values[i]) // the middle contains value?
return true;
else if (value < values[i]) // would be value to the left of i
return search(value, values, i);
else // so it would be to the right; no need of if (value > values[i])
return search(value, values + i + 1, n - i - 1);
}
This function, which has the same signature that yours, reduce the searching range through the number of elements n
and the base of array pointer. When the search is performed to the left of center, the numbers of elements decrease to i
and when the search is performed to the right the array base address if increased by i + 1
and the number of elements is decreased by i + 1
.
Note that your third if
is not necessary; if it is reached, then the predicate always will be true, because value
is not equal neither less than value[i]
, so it is greater.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.