For an assignment at my college I am trying to program an interpolation search in C. I have looked through Google and found an algorithm on Wikipedia and adjusted it to fit the program but it seems to hang when the variable 'target' stays at a fixed number and never changes. Basically the object of the program is to get a number from Array2 and then search Array1 via interpolation until it matches or doesn't match the number in Array2
Here is the code
In bold I highlighted where I think the problem is, is that a valid equation?
Thanks to anyone who can help me with this
Here is the code
Spoiler :
void Interpolation(int Idx1, int Idx2, int Array1[1000000], int Array2[1000000], int i)
{
int target, wanted, min, max, Found = 0, Length, NotFound = 0;
bool Match = false;
Length = 100000*i;
Idx1=0;
Idx2=0;
printf("Commencing interpolation search\n");
min = 0;
max = Length - 1;
wanted = Array2[Idx2];
while (Array1[min] < wanted && Array1[max] >= wanted && !Match)
{
target = min + ((wanted - Array1[min]) * (max - min)) / (Array1[max] - Array1[min]);
if (Array1[target] < wanted)
{
min = target + 1;
}
else if (Array1[target] > wanted)
{
max = target - 1;
}
else
{
Found++;
Match= true;
}
Idx2++;
}
if (Array1[min] == wanted)
{
Found++;
}
else
{
NotFound++;
}
printf("%d numbers were matched up, %d were not matched up\n",Found, NotFound);
system("Pause");
}
{
int target, wanted, min, max, Found = 0, Length, NotFound = 0;
bool Match = false;
Length = 100000*i;
Idx1=0;
Idx2=0;
printf("Commencing interpolation search\n");
min = 0;
max = Length - 1;
wanted = Array2[Idx2];
while (Array1[min] < wanted && Array1[max] >= wanted && !Match)
{
target = min + ((wanted - Array1[min]) * (max - min)) / (Array1[max] - Array1[min]);
if (Array1[target] < wanted)
{
min = target + 1;
}
else if (Array1[target] > wanted)
{
max = target - 1;
}
else
{
Found++;
Match= true;
}
Idx2++;
}
if (Array1[min] == wanted)
{
Found++;
}
else
{
NotFound++;
}
printf("%d numbers were matched up, %d were not matched up\n",Found, NotFound);
system("Pause");
}
In bold I highlighted where I think the problem is, is that a valid equation?
Thanks to anyone who can help me with this