[英]how can i return the index of an integer in an array using a recursive function?
i have to find the index of an integer in an array using a recursive function this is what i have up to now:我必须使用递归 function 在数组中找到 integer 的索引,这是我到目前为止所拥有的:
#include <iostream>
int linear_search(int array[], int choice_, int size) {
if (array[choice_] >= 0) {
return array[choice_];
}
else {
return -1;
}
}
for example if the:例如,如果:
int array[]= {3,4,7,8};
if i type that i want to search for number 3 it should give me the index of 0 but instead it gives me the number 8. can i get some advise please?如果我输入我想搜索数字 3,它应该给我索引 0,但它给我数字 8。我能得到一些建议吗?
Recursion is about narrowing down a problem to a smaller problem.递归是将问题缩小为较小的问题。
In the case of this particular search, try to think of it in terms of the found-item being either in the first position in the array, or maybe in the remainder of the array.在此特定搜索的情况下,尝试根据找到的项目来考虑它,它位于数组中的第一个position 中,或者可能位于数组的其余部分中。 This then reduces the problem to searching a smaller array, until you either find the item or you hit the trivial case of an empty array.
然后,这将问题减少到搜索较小的数组,直到您找到该项目或遇到空数组的琐碎情况。
int linear_search(int array[], int choice_, int size)
{
// Array is empty -- not found
if (size <= 0)
return -1;
// Found at position 0
if (array[0] == choice_)
return 0;
// TODO: Search position in remaining array
int pos = linear_search(/* USE BRAIN HERE */);
// TODO: You may want to do something to the result before returning it
return pos;
}
As you can see, I've left some parts for you to fill in. I have faith in you.如你所见,我留了一些部分让你填写。我对你有信心。 You can do it!
你能行的!
Happy coding!快乐编码!
This is binary search.这是二分查找。 It is a recursive function.
它是一个递归 function。
int binary_search_recursive(const int b[], int search_key, int low, int high)
{
if (low > high)
return -1;
int mid = low + (high - low) / 2;
if (search_key == b[mid])
return mid;
else if (search_key < b[mid])
return binary_search_recursive(b, search_key, low, mid - 1);
else
return binary_search_recursive(b, search_key, mid + 1, high);
}
This code is wrong这段代码是错误的
if (array[choice_] >= 0) {
return array[choice_];
When you do array[choice_]
you are looking at the array value at index choice_
.当您执行
array[choice_]
时,您正在查看索引choice_
处的数组值。 That is not what you want.那不是你想要的。 Instead you want to find out if the array at some index is equal to
choice_
.相反,您想找出某个索引处的数组是否等于
choice_
。 That would be if (array[some_index] == choice_) return some_index;
if (array[some_index] == choice_) return some_index;
Further, to make this a recursive approach, the function needs to call itself.此外,为了使其成为递归方法,function 需要调用自身。 Your code doesn't do that so it's not recursive.
您的代码没有这样做,因此它不是递归的。
Here is a recursive approach:这是一种递归方法:
int searchArray(const int* array, const int choice_, const int size)
{
if (size > 0)
{
if (array[size-1] == choice_) return size-1; // Hit! Return the index
return searchArray(array, choice_, size-1); // Recursive call - pretend array is 1 shorter
}
return -1;
}
The code looks at the last element in the array.代码查看数组中的最后一个元素。
If it is a hit it simply returns the index.如果它是一个命中它只是返回索引。
If it is not a hit, it calls itself but decrements size
to pretend that the array is one element shorter.如果不是命中,它会调用自身,但会减小
size
以假装数组短一个元素。
In this way it goes from the end of the array towards the start of the array while looking for choice_
.通过这种方式,它在寻找
choice_
时从数组的末尾向数组的开头移动。
BTW: Notice that it's a very bad idea to use recursion for this task - never do that!顺便说一句:请注意,对这个任务使用递归是一个非常糟糕的主意——永远不要那样做! Use a simple
for
loop.使用简单
for
循环。 But I guess this is just another example of a very bad homework task...但我想这只是一个非常糟糕的家庭作业的另一个例子......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.