【问题标题】:C++ Binary Search - Array Position VariablesC++ 二分搜索 - 数组位置变量
【发布时间】:2015-04-13 20:57:55
【问题描述】:

这应该只是一个愚蠢的小问题(如果我能说得好的话)。在查看我教授的示例并观看 YouTube 视频时,我注意到当人们设置二进制搜索时,他们声明但不定义数组位置的变量(例如,第一、中、最后或低、中、高)。

教授的例子:

int bsearch(const int list[], int first, int last, int searchItem){
  if(first <= last){
    int mid = (first + last) / 2;
    .......

如果不为 first/last/mid 设置值,这如何/为什么会起作用?


好的,这些回复是有道理的。但是,在我教授的例子中,我很难理解这种情况发生在哪里。我看到他在哪里返回 bsearch(),我认为仍然是未定义的参数,后来调用 binarySearch(),但只有 3 个参数。我看到我被否决了;我不确定这个人为什么或在哪里希望我转向编程问题。无论如何,这是完整的代码:

#include <iostream>
using namespace std;

int bsearch(const int list[], int first, int last, int searchItem)
{
  if(first <= last) {
    int mid = (first + last) / 2;
    if(list[mid] == searchItem)
      return mid;
    else
      if(list[mid] > searchItem)
        return bsearch(list, first, mid-1, searchItem);
      else
        return bsearch(list, mid+1, last, searchItem);
  }
  else
    return -1;
}

// Non-recursive shell for the recursive binary search function.
// This function does not use "first" and "last" as its parameters.
int binarySearch(const int list[], int listLength, int searchItem)
{
  return bsearch(list, 0, listLength-1, searchItem);
}

int main() // testing recursive binarySearch function
{
  int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
  int item;
  cout << "Search item? ";
  cin >> item;
  cout << binarySearch(a, 10, item) << endl;
  return 0;
}

【问题讨论】:

  • 函数调用时提供的值。
  • mid 已定义。函数参数是声明的,调用时会定义。
  • 好吧,我想我意识到“last”和“searchItem”来自:“cout

标签: c++ binary-search


【解决方案1】:

变量在调用时传递给函数。例如:

int main()
{
    int list[] = [1,2,3,4,5];
    cout << bsearch(list, 0, 4, 2);
    return 0;
}

【讨论】:

    【解决方案2】:

    我们用第三行的语句定义mid

    int mid = (first + last) / 2;
    

    至于其他,我们通过调用函数来定义firstlast。例如:

    a = bsearch(my_list, 1, 100, 55);
    

    根据int bsearch(const int list[], int first, int last, int searchItem)这相当于说

    int first = 1;
    int last = 100;
    int searchitem = 55;
    

    我们这样做的部分原因是我们并不总是希望为每个二分搜索定义相同的值,但更重要的是,它允许我们调用二分搜索recursively

    那么你教授的例子呢?

    让我们跟踪函数调用,看看会发生什么!

    main() 我们有这行代码:

    cout << binarySearch(a, 10, item) << endl;
    

    当我们看binarySearch时,函数的结构如下:

    int binarySearch(const int list[], int listLength, int searchItem)
    

    根据前面的例子,你也可以这样认为

    const int list[] = a; //for now you can think of it as "= (the contents of a)", though it works a little different in reality
    int listLength = 10;
    int searchItem = item;
    

    但是现在我们进行另一个函数调用:

    return bsearch(list, 0, listLength-1, searchItem);
    

    但是等等!基于我们上一个示例,并且知道 bsearch (bsearch(const int list[], int first, int last, int searchItem)) 的结构,我们可以看到这些变量也被定义了!

    在某种程度上,你可以这样想!

    //== main() ==
    
       int a[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
       int item;
       cin >> item;
    
    
    //== int const list[] v       v int searchItem ==
    //==     binarysearch(a, 10, item)             ==
    //==       int listLength ^                    ==
    
       const int list[] = a;
       int listLength = 10;
       int searchItem = item;
    
    //== int const list[] v          v int last                     ==
    //==         bsearch(list, 0, listLength -1, searchItem)        ==
    //==             int first ^                   ^ int searchItem ==
    
       const int list_bsearch = list;
       int first = 0;
       int last = listLength -1;
       int searchItem_bsearch = searchItem;
    

    SO,比如我们定义searchItem的方式,你只要按照定义就好了。

    searchItem (bsearch) = searchItem (binarySearch) = item

    注意 binarySearch 和 bsearch 是不同的函数,具有不同的参数。 “为什么这样做”背后的原因与递归有关,完全值得另一个问题。

    还是卡住了?

    这是一个非常简单的例子:

    int fooAdd(int a, int b){
    
       //a and b are ints
       //by calling fooAdd(2, five) we set a = 2, and b = five (which was an int that happened to have the bvalue 5;
    
       return a +b;
    }
    
    int main(){
       int five = 5;   
       cout << fooAdd(2, five);
       return 0;
    }
    

    附:回应您的“我知道我被否决了;我不确定这个人为什么或希望我在哪里回答编程问题”的俏皮话。

    stackoverflow 的许多人都喜欢帮助你,但有时问题写得不是很清楚,或者有太多小问题无法真正帮助你。

    不要认为通过投反对票,我们会阻止您提出问题。试着想象如何更好地问你的问题!

    【讨论】:

    • 这在理论上是有道理的,但在我教授的例子中,我无法辨别这发生在哪里。
    • @shane,我会尝试找出答案以更好地解释它,好吗?抱紧!
    • 哇。我非常感谢你的彻底回应。我正在为考试而学习,直到我了解它是如何工作的(我的思想是如何连接的),我才能进一步进步。另外,从这里开始,我会尝试更好地表达我的问题。我在措辞上付出了很多努力——只是有时我对这个话题的理解不够,甚至不知道该问什么或如何问。我什至说什么的唯一原因是不久前我几乎被禁止提问。作为一名 IT 学生,这将是一个重大损失。我绝对不是巨魔,我尽量考虑周到。再次感谢。
    • @shane 太棒了!很高兴我能提供帮助,您可以通过接受此答案来帮助我(以及任何提出类似问题的人)。
    【解决方案3】:

    我猜提供的函数是递归。因此,在您进入递归(函数调用自身)之前,您必须从其他地方调用该函数(例如:您从 main 调用 bsearch,然后它在 main 中 first 和 last 设置为某些值)

    int main ()
    {
       bsearch(list, 0, 4, 2);
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-30
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 2017-04-16
      • 1970-01-01
      相关资源
      最近更新 更多