试写出折半查找的递归算法。


试写出折半查找的递归算法。

正确答案:int BinSrch(rectype r[ ],int k,low,high) //在长为n的有序表中查找关键字k,若查找成功,返回k所在位置,查找失败返回0。 {if(low≤high) //low和high分别是有序表的下界和上界 {mid=(low+high)/2; if(r[mid].key==k)return (mid); else if(r[mid].key>k)return (BinSrch(r,k,mid+1,high)); else return (BinSrch(r,k,low,mid-1)); } else return (0);//查找失败。 }//算法结束


Tag:数据结构 递归 算法 时间:2024-01-19 16:04:16

相关答案

热门答案