Ternary search in C

前端 未结 4 907

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

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 03:36

    @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.

提交回复
热议问题