That seemed to work for all the values I could throw at it. using rand() by itself doesn't seem to produce large enough values, so I changed it to rand() * rand(), as in my previous tests.
I think you have the num_tries counter in the wrong place -- it doesn't count the number of tries correctly. In the code below I also typecast the array a to int when making the comparisons to insure integer test rather than double test.
if ((k < lb) || (k > ub))
{
return -1;
}
(*num_tries)++;
if ((int)(a[k]) == key)
{
return k;
}
else if ((int)(a[k]) > key)
{
// *num_tries = *num_tries + 1;
return search(a, lb, k-1, key, num_tries);
}
else
{
// *num_tries = *num_tries + 1;
return search(a, k+1, ub, key, num_tries);
}