I want to do a ternary search for integers in C ... I have tried it...but it\'s not working well for particular cases. Please help me to remove the bugs from the following progr
@uncleo makes a good point - and provides a decent solution.
The bigger problem, though, is that if you search for a number that isn't in the array, there is nothing to stop the recursion - it continues until it crashes.
Also, the design of tsearch()
is suspect; the code shown says it is a void function, yet you use return(tsearch(...));
several times (as well as a couple of plain return;
statements). Surely, the function should return an int
and the two plain return;
statements should return m1
or m2
? You also need to deal with the case where the range is degenerate (i >= j
) which means the value is not present - maybe you can return -1 or something similar for that case.