【问题标题】:binar search function not returning # of comparisons二分查找函数不返回比较次数
【发布时间】:2022-01-08 00:14:51
【问题描述】:

给定一个整数和一个排序的整数数组,编写一个名为binary_search 的二分查找函数,该函数打印执行二分查找的比较次数。该函数应采用 3 个参数:

  • 搜索的号码,
  • 整数数组,以及
  • 数组中的元素数。

如果搜索的数字不在数组中,则该函数应返回最大搜索次数以确定该元素不在数组中。

这是一个调用函数的示例:

#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。

【问题讨论】:

  • 那是因为您正在返回项目的索引。您绝不会计算比较次数。

标签: c++ search binary comparison binary-search


【解决方案1】:

您的代码存在一些问题。首先关闭这条线:

return binary_search( elements,lst, mid);

由于您的第一个参数是您要搜索的数字,因此它应该始终是相同的 search_value。

然后,二分搜索背后的想法是每次调用都会将元素数量减半,因此您无需随时更改第三个参数,它始终是 elements/2 + 1。

最后,如 cmets 中所述,您的函数不会返回比较次数,而是返回数字“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;
}

【讨论】:

    【解决方案2】:

    我会提出以下解决方案:

    [Demo]

    #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 个元素。这是使用 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);
    }
    
    • 对于大于枢轴元素的值,我们进行 3 次比较(== 0、> search_value、
    //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);
    }
    
    • 否则,该值等于枢轴元素,我们返回 3 个比较(== 0、> search_value、
    else
    {
        return 3;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 2018-03-27
      • 1970-01-01
      相关资源
      最近更新 更多