for some reason this binary search code keeps returning true, when providing the haystack from generate.c, even if the "needle" isnt there. when manually inputing the haystack, the code runs perfectly. bool search(int value, int values[], int n) {
// TODO: implement a searching algorithm
// implement a binary search.
int low = values[0];
int high = values[n - 1];
while(low <= high)
{
//make a mid
int mid = (low + high) / 2;
//if value is less than mid
if(value < mid)
{
high = (mid - 1);
}
//if value is more than mid
if(value > mid)
{
low = (mid + 1);
}
//if value = mid, return true;
if(value == mid)
{
return true;
}
}
return false;
//end of search }