c++ - 排序数组c++中的递归二进制搜索

标签 c++ arrays algorithm recursion binary-search

我必须编写一个递归函数来搜索已排序的数组。

#include <iostream>
using namespace std;

int find(int value, int* folge, int max) {

}


int main() {
    int const n = 7;
    int wert1 = 4, wert2 = 13, wert3 = 2, wert4 = 25;
    int folge[n] = {3,4,9,13,13,17,22};
    wert1 = find(wert1, folge, n);
}

这是给我们的代码部分,我们必须完成它。
如果您有 4 个可用变量,我知道该怎么做。 (最小值和最大值) 但是我只有三个,有没有办法编辑给下一个函数的数组的起点?

最佳答案

假设您有四参数版本:

find(value, array, min, max);

使用三参数版本你可以调用:

find(value, array + min, max);

请注意,数组的第 max 个元素实际上是 array + min 的第 max-min 个元素,因此取决于您的实现你可能想打电话

find(value, array + min, max - min);

关于c++ - 排序数组c++中的递归二进制搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34664009/

相关文章:

java - 将数据插入二维 byte[] 数组

c - 从四个中找到两个最小值?

c++ - 是否有合并数值范围的有效算法?

c# - C# 类中的数组引用

c++ - 如何从具有不同构造函数的父类(super class)访问子类的成员?

javascript - RegExp/JavaScript : Split string on multiple characters, 保留分隔符而不使用lookbehind

algorithm - 对双对数组进行分组的最快方法是什么

c++ - C++ 上的哈希聚合

c++ - 一旦在任何窗口内运行动画,qt QML 中的多个窗口就会开始闪烁/闪烁

C++ 奇怪的函数指针行为