This is an attempt at binary search. n is the user input (from main script).
This code compiles but doesn't find the needle in the haystack. I'm a bit lost and would welcome assistance.
bool search(int value, int values[], int n)
{
int min = values[0];
int max = values[n-1];
int mid = (max + min) / 2;
if (n >= 0)
{
if (values[mid] == value)
{
return true;
}
else if (values[mid] > value)
{
for (int i=0; i < (values[mid])-1; i++) //to search left half
{
if (i == value)
{
return true;
}
}
}
else if (values[mid] > value)
{
for (int i = (values[mid])+1; i < (values[max])-1; i++)
//search right half
{
if (i == value)
{
return true;
}
}
}
else
{
return false;
}
}
return false;
}