所以..我已经了解了二进制搜索及其工作原理,甚至在没有用户输入的情况下使用常量数组对其进行了尝试,但是现在我试图应用矢量而不是数组来让用户输入两个值从 vector 中搜索数字的列表以及要搜索的目标。在这里,我在使用数组时使用了普通的分而治之
using namespace std;
int Binary_search(int x[],int size,int target){
int maximum= size-1;
int minimum = 0;
int mean;
while (maximum>minimum){
mean = (maximum+minimum)/2;
if (x[mean] == target){
cout << "The number you're looking for is found! \n";
return mean;
}
else if(x[mean] > target){
maximum = (mean-1);
}
else{
minimum = (mean+1);
}
}
return -1;
}
int main(){
int x[]={1,2,3,4,5};
int a=sizeof(x)/sizeof(x[0]);
int target=4;
int show=Binary_search(x,a,target);
if (show != -1){
cout << "Your result is in the index: " << show;
}
return 0;
}
我的问题是,我使用vector做了几乎相同的方法,但是它显示了无限数量的**您的结果在索引处发现:**(错误索引数)。或者它根本不显示任何结果,甚至表明未找到结果,每次都会有所不同。这是使用向量
的时候#include <iostream>
#include <vector>
using namespace std;
int Binary_search(vector<int>x,int target){
int maximum=(x.size())-1;
int minimum = 0;
int mean;
while (maximum>minimum){
mean = (maximum+minimum)/2;
if (x[mean] == target){
cout << "The number you're looking for is found! \n";
}
else if(x[mean] > target){
maximum = (mean-1);
}
else{
minimum = (mean+1);
}
}
return -1;
}
int main(){
unsigned int i;
int n;
vector<int>x;
cout << "Enter the amount of numbers you want to evaluate: ";
cin >> i;
cout << "Enter your numbers to be evaluated: " << endl;
while (x.size() < i && cin >> n){
x.push_back(n);
}
int target;
cout << "Enter the target you want to search for in the selected array \n";
cin >> target;
int show = Binary_search(x,target);
if (show == -1){
cout << "Your result is not found ! ";
}
else{
cout << "Your result is in the index: " << show;
}
return 0;
}
所以我认为问题出在int maximum=(x.size())-1;
这部分,也许是关于如何使用向量的大小?有人可以启发我这个问题
答案 0 :(得分:1)
您需要在此行之后return mean
cout << "The number you're looking for is found! \n";
就像在阵列版本中一样。
此外,如注释中所述,这仅在用户输入排序数据时才有效。
答案 1 :(得分:1)
问题:
cout << "The number you're looking for is found! \n";
return mean;
else if(x[mean] > target)
{
maximum = mean;
}
else
{
minimum = mean;
}
为什么走得太快可能是个问题? 尝试
vector<int>x{1,2,3,4,5,6};
int target = 4;
答案 2 :(得分:1)
好吧,其他答案也有助于解决问题,但如果您不知道,可以在C++
中使用内置函数来执行 binary search 使用它们。
我将列出与功能相关的二进制搜索:
这是一个演示先前功能的代码示例:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iostream>
typedef std::vector<int>::iterator iter;
int main() {
std::vector<int> vec = {10, 20, 30, 30, 20, 10, 10, 20};
// sort the data
// the data will be:
// 10, 10, 10, 20, 20, 20, 30, 30
std::sort(vec.begin(), vec.end());
// index of the first element, greater than or equal to 20
iter low = std::lower_bound(vec.begin(), vec.end(), 20);
// index of the first element, greater than 20
iter high = std::upper_bound(vec.begin(), vec.end(), 20);
std::cout << "index of first element, greater than or equal to 20 is: " << (low - vec.begin()) << '\n';
std::cout << "index of first element, greater than to 20 is: " << (high - vec.begin()) << '\n';
// classic binary search
// check whether a givin value exists in the array or not
if (std::binary_search(vec.begin(), vec.end(), 99)) {
std::cout << "Found\n";
} else {
std::cout << "Not found\n";
}
}