【问题标题】:Binary Search using a vector使用向量的二分搜索
【发布时间】:2020-03-13 20:41:11
【问题描述】:

所以.. 我已经了解了二进制搜索及其工作原理,甚至尝试使用常量数组而不需要用户的任何输入,但现在我尝试应用向量而不是数组来让用户输入两者的值从 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;
}

我的问题是我使用向量做了几乎相同的方法,但它显示了无限数量的 **Your result is found at the index: ** (number of wrong index) 。或者根本不显示任何结果,甚至显示未找到结果,每次都以某种方式不同。这是在使用向量时

#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;这部分,也许是关于如何使用向量的大小?有人可以启发我吗

【问题讨论】:

  • 您是否考虑过您的阵列版本总是损坏,并且使用vector 检测到损坏?其次,为什么不将数据硬编码到向量中并进行测试,就像在数组版本中一样?
  • 请注意,二分查找仅适用于已排序的数据集。如果用户输入未排序的数据,那么您需要先对向量进行排序,然后才能对其进行二进制搜索。
  • 好点,谢谢! NathanOliver 我不明白你所说的总是坏是什么意思,你能解释一下@PaulMcKenzie
  • 有时使用数组会隐藏一个个或其他细微的错误。使用向量时,程序的行为可能会有所不同,此时隐藏的错误就会暴露出来。

标签: c++ search vector


【解决方案1】:

嗯,其他答案有助于解决问题,但如果您不知道,C++ 中有用于执行 二分搜索 的内置函数,您可以使用它们.

我将列出函数相关的二分查找:

  • sort:你只能对一个排序的数据使用二分查找,所以在搜索之前你必须保证数据是排序好的。
  • lower_bound:这个函数返回一个迭代器到第一个大于或等于值的元素。
  • upper_bound:这个函数返回一个迭代器到第一个大于值的元素。
  • binary_search:: 这个函数返回一个boolean,不管这个值是否被找到(和你的程序一样)。

这是一个演示前面功能的代码示例:

#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";
    }

}

【讨论】:

  • 也谢谢!但这肯定包括一个预定义的算法,不是吗?
  • 对不起,我没有清楚地理解你的问题。 在开始时包含预定义算法,如果您的意思是应该在开始时定义算法并实施,那么答案是NO。这些是内置功能,您可以立即使用。
【解决方案2】:

此行后需要return mean

cout << "The number you're looking for is found! \n";

就像你在数组版本中所做的那样。

此外,如 cmets 中所述,这仅在用户输入排序数据时才有效。

【讨论】:

    【解决方案3】:

    问题:

    1. 没有回报。添加
    cout << "The number you're looking for is found! \n";
    return mean;
    
    1. 速度太快,可能会跳过一些数字
    else if(x[mean] > target)
    {
        maximum = mean;
    }
    else
    {
        minimum = mean;
    }
    

    为什么过快可能是个问题? 什么时候试试

    vector<int>x{1,2,3,4,5,6};
    int target = 4;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多