[英]Recursive binary search in sorted array c++
I have to write a recursive function which searches through a sorted array. 我必须编写一个通过排序数组搜索的递归函数。
#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);
}
This is the part of code that was given to us and we have to finish it. 这是提供给我们的代码部分,我们必须完成它。
I know how to do this if you have 4 variables available. 如果您有4个变量,我知道该怎么做。 (min and max) But I only have three, is there a way to edit the start point of the array which is given to the next function?
(最小和最大)但是我只有三个,有没有办法编辑分配给下一个函数的数组的起点?
Suppose you have your four-param version: 假设您有四个参数的版本:
find(value, array, min, max);
with the three-parameter version you can call: 使用三参数版本,您可以调用:
find(value, array + min, max);
Note that the max
'th element of array is actually max-min
th element of array + min
, so depending on your implementation you may want to call 请注意,数组的第
max
个元素实际上是数组的第max-min
个元素array + min
,因此根据您的实现,您可能需要调用
find(value, array + min, max - min);
The function can be written the following way 该函数可以通过以下方式编写
#include <iostream>
int find( int value, int* folge, int max )
{
if ( max == 0 ) return -1;
int middle = max / 2;
if ( folge[middle] == value ) return middle;
bool lower = value < folge[middle];
int n = lower ? find( value, folge, middle )
: find( value, folge + middle + 1, max - middle - 1 );
return n != -1 && !lower ? n + middle + 1: n;
}
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};
std::cout << wert1 << " = " << find(wert1, folge, n) << std::endl;
std::cout << wert2 << " = " << find(wert2, folge, n) << std::endl;
std::cout << wert3 << " = " << find(wert3, folge, n) << std::endl;
std::cout << wert4 << " = " << find(wert4, folge, n) << std::endl;
return 0;
}
The program output is 程序输出为
4 = 1
13 = 3
2 = -1
25 = -1
Or one more test program 或另外一个测试程序
#include <iostream>
int find( int value, int* folge, int max )
{
if ( max == 0 ) return -1;
int middle = max / 2;
if ( folge[middle] == value ) return middle;
bool lower = value < folge[middle];
int n = lower ? find( value, folge, middle )
: find( value, folge + middle + 1, max - middle - 1 );
return n != -1 && !lower ? n + middle + 1: n;
}
int main()
{
int const n = 7;
int folge[n] = {3,4,9,13,13,17,22};
for ( int x : { 2, 3, 4, 9, 13, 17, 22, 25 } )
{
std::cout << x << " -> " << find( x , folge, n ) << std::endl;
}
return 0;
}
Its output is 它的输出是
2 -> -1
3 -> 0
4 -> 1
9 -> 2
13 -> 3
17 -> 5
22 -> 6
25 -> -1
find( &folge[1],...) // ignore first element
第n个元素的地址是一个大小为Size-n的数组
You can change the value of the int pointer folge
您可以更改int指针的
folge
int find(int value, int* folge, int max) {
if (max == 0) {
return -1;
}
int mid=max/2;
if (value == folge[mid])
{
return mid;
}
int base=0;
if (value < folge[mid])
{
max=mid-1;
}
else
{
max-=(mid+1);
base=mid+1;
}
int ret=find(value,folge+base,max);
if (ret == -1)
{
return -1;
}
return ret+base;
}
If folge
points to the first element, then folge+1
will point to the second. 如果
folge
指向第一个元素,则folge+1
指向第二个元素。
int find(int value, int* folge, int max)
{
int m = max/2;
if (0 == max)
{
return -1;
}
if (value == folge[m])
{
return value;
}
else if (value < folge[m])
{
return find(value, folge, m);
} else
{
return find(value, folge + m + 1, max - m - 1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.