I have an issue with the "find" problem. When I test my program, it seems to get stuck in an infinite loop (nothing is prompted, there is just a blank space and I have to kill the program with ctrl c). I have read so many threads, tried many differents fixes, but nothing seems to work. I feel it's something in my sorting algorithm, but I'm not completely sure. Can anyone point me in the right direction? I'm really lost on this one!
Here is the searching algorithm:
#include <cs50.h>
#include "helpers.h"
int end, beg, mid;
bool search(int value, int values[], int n)
{
end = n-1;
beg = 0;
mid = (end + beg /2);
while (end >= beg)
{
if (values[mid] == value)
{
return true;
}
else if (values[mid] < value)
{
beg = mid + 1;
}
else
{
end = mid - 1;
}
}
return false;
}
And here is my selection sort algorithm, which I think is the one with the problem:
int small, swap;
void sort(int values[], int n)
{
for (int i=0; i <= n-2; i++)
{
small = i;
for (int j = i + 1; j < n; j++)
{
if (values[j] < values[small])
{
small = j;
}
}
if (small != i)
{
swap = values[small];
values[small] = values[i];
values[i] = swap;
}
}
return;
}
Thank you so much for your help, I'm really getting crazy on this one!