[英]binar search function not returning # of comparisons
给定一个 integer 和一个排序的整数数组,编写一个名为binary_search
的二进制搜索 function 打印执行二进制搜索的比较次数。 function 应取 3 个 arguments:
如果搜索的数字不在数组中,则 function 应返回最大搜索次数以确定该元素不在数组中。
这是对 function 的示例调用:
#include <iostream>
using namespace std;
//Function for binary_search
int binary_search(int search_value, int lst[], int elements)
{
//Dividing the array elements to its half
int mid = elements / 2;
//Condition to check search value is less then list of mid element and return it.
if (lst[mid] > search_value)
return binary_search( elements,lst, mid);
//Condition to check search value is greater then list of mid element and return it.
else if (lst[mid] < search_value)
return binary_search( search_value,&lst[mid], (elements + 1)/2);
else
return mid;
}
int main()
{
int lst[] = {0, 1, 2, 18, 19, 20, 25};
std:cout << binary_search(20, lst, 7);
}
当我搜索20
时,它返回找到的索引,即 5,而不是比较次数,应该是 2。
您的代码存在一些问题。 首先关闭这条线:
return binary_search( elements,lst, mid);
由于您的第一个参数是您要搜索的数字,因此它应该始终是相同的 search_value。
然后,二进制搜索背后的想法是每次调用都会将元素数量减半,因此您无需随时更改第三个参数,它始终是 elements/2 + 1。
最后,如评论中所述,您的 function 不返回比较次数,而是返回数字“mid”。 由于每次调用都进行一次比较,因此您基本上必须找到调用次数。 因此,您的递归基本情况(当您找到要搜索的数字时)应该返回 1,因为要找到它,您已经进行了比较。 然后你只需返回 1 + 已经进行的递归调用的数量。
最后代码应该是这样的,
int binary_search(int search_value, int lst[], int elements)
{
int mid = elements / 2;
if (lst[mid] > search_value)
return 1 + binary_search( search_value, lst, elements/2 + 1);
else if (lst[mid] < search_value)
return 1 + binary_search( search_value, &lst[mid], elements/2 + 1);
else
return 1;
}
我会提出以下解决方案:
#include <iostream> // cout
//Function for binary_search
int binary_search(int search_value, int lst[], int elements)
{
//Base case: not found
if (elements == 0) { return 1; }
//Dividing the array elements to its half
int mid = elements / 2;
//Condition to check search value is less then list of mid element and return it.
if (lst[mid] > search_value)
{
return 2 + binary_search(search_value, lst, mid);
}
//Condition to check search value is greater then list of mid element and return it.
else if (lst[mid] < search_value)
{
return 3 + binary_search(search_value, &lst[mid + 1], elements - mid - 1);
}
else
{
return 3;
}
}
int main()
{
int lst[] = {0, 1, 2, 18, 19, 20, 25};
std::cout << "lst: ";
for (auto&& i : lst)
{
std::cout << i << " ";
}
std::cout << "\n";
for (auto&& i : lst)
{
std::cout << "binary_search(" << i << ", lst, 7): "
<< binary_search(i, lst, 7) << "\n";
}
std::cout << "binary_search(10, lst, 7): "
<< binary_search(10, lst, 7) << "\n";
}
// Outputs:
//
// lst: 0 1 2 18 19 20 25
// binary_search(0, lst, 7): 7
// binary_search(1, lst, 7): 5
// binary_search(2, lst, 7): 8
// binary_search(18, lst, 7): 3
// binary_search(19, lst, 7): 8
// binary_search(20, lst, 7): 6
// binary_search(25, lst, 7): 9
// binary_search(10, lst, 7): 9
解释:
binary_search
添加了一个基本情况。 这是使用 0 个元素的调用的安全案例,但也是递归调用期间未找到元素时的基本案例。 由于我们正在进行比较,因此我们返回 1。if (elements == 0) { return 1; }
lst[mid]
的值,我们进行 2 次比较 (== 0, > search_value) 以及递归调用返回的比较。//Condition to check search value is less then list of mid element and return it. if (lst[mid] > search_value) { return 2 + binary_search(search_value, lst, mid); }
//Condition to check search value is greater then list of mid element and return it. else if (lst[mid] < search_value) { return 3 + binary_search(search_value, &lst[mid + 1], elements - mid - 1); }
else { return 3; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.