【发布时间】:2011-02-08 12:53:56
【问题描述】:
为二分搜索创建一个递归函数。
此函数接受排序后的数组和要搜索的项目,并返回项目的索引(如果项目在数组中),或者返回 -1(如果项目不在数组中)。
此外,编写一个测试程序来测试您的功能。
template <class elemType>
int orderedArrayListType<elemType>::binarysearch
(const elemType& item) const
{
int first= 0;
int last = length -1;
int mid;
int list[];
int BinarySearch(,Type & Item, int first, int last)
bool found = false;
while (first <= last && !found){
mid = (first + last) / 2;
if (list[mid] > item)
return BinarySearch(list, item, first, mid -1)
found = true;
else if (list[mid] > item)
return BinarySearch( list, item, first, mid -1)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}
【问题讨论】:
-
这里的交易是您编写自己的作业解决方案,向我们询问您在代码中遇到的具体问题。
-
一定是一年中的那个时候。
-
导师不知道,还是不想给你做作业?
-
请您的老师用您通常交流的任何语言解释问题,然后为我们翻译。
-
我会说英语,她不会哈哈,但我重新编辑了我的问题并发布了我到目前为止所做的事情,感谢您在本网站上对新手的帮助
标签: c++ recursion binary-search