bool search(int value, int values[], int n)
{
int left = 0, right = n - 1;
// TODO: implement a searching algorithm
while (n > 0)
{
int middle = (left + right) / 2;
if (value == middle)
{
return true;
}
if (left == right && value != middle)
{
break;
}
else if (value > middle)
{
left = middle + 1;
}
else if (value < middle)
{
right = middle - 1;
}
}
return false;
}
/**
* Sorts array of n values.
*/
void sort(int values[], int n)
{
// TODO: implement an O(n^2) sorting algorithm (selection sort)
for (int i = 0; i < n; i++)
{
int min = values[i];
// i2 as a second counter
for (int i2 = 0; i2 < n; i2++)
{
if (min > values[i2])
{
min = values[i2];
int temp = values[i];
values[i] = min;
min = temp;
}
}
return;
}
}
when I run find.c it doesn't find the needle (but it finds few) and I don't know where the problem is. (I didn't change anything in find.c). Some help please.