简体   繁体   English

C语言中的二进制搜索递归算法问题

[英]Binary Search Recursion Algorithm Issue In C

Ok, so I'm given the function 好的,所以我给了这个功能

int bin(int value, int size, int array[])

I am supposed to find "value" within "array[]", but the issue at hand here is that in most cases, we have something along the lines of 我应该在“array []”中找到“value”,但是这里的问题在于,在大多数情况下,我们有类似的东西。

int bin(int value, int max, int min, int array[])

The recursion, logically, is a lot easier on this part due to the fact I can still pass the number I am at, as well as remember the size of the array. 从逻辑上讲,这部分的递归要容易得多,因为我仍然可以传递我所在的数字,以及记住数组的大小。

int bin(int array[], int value, int min, int max)
{
    if(max < min)
        return -1;
    else
    {
        int mid = min + (max - min)/2;

        if(array[mid] > value)
            return bin(array, value, min, mid-1);
        else if(array[mid] < value)
            return bin(array, value, mid+1, max);
        else
            return mid;
    }

But since I can only pass 1 integer, how exactly would I adjust this algorithm for it? 但由于我只能传递1个整数,我究竟会如何调整这个算法呢? In essence, I would only be able to do something like this, but I KNOW it will not logically work. 从本质上讲,我只能做这样的事情,但我知道它在逻辑上不会起作用。 Is there a way, though, that I can see the size of the array? 有没有办法,我可以看到数组的大小? I tried it, but the numbers weren't crunching right. 我尝试过,但数字没有正确处理。

   int bin(int array[], int value, int size)
    {
            int mid = size/2;

            if(array[mid] > value)
                return bin(array, value, size-(size/2));
            else if(array[mid] < value)
                return bin(array, value, size+(size/2));
            else
                return mid;
    }

You need to explicitly pass the base as well: 您还需要明确传递基数:

int
bin(int array[], int value, int size)
{
        int mid = size/2;

        if(array[mid] > value)
            return bin(array, value, size/2);
        else if(array[mid] < value)
            return bin(&array[mid], value, size/2);
        else
            return mid;
}

note the "&array[mid] in the "if(array[mid] < value)" case 请注意“if(array [mid] <value)”情况下的“&array [mid]”

each time, you have the correct half of the array to search, using base + offset vice min/max indices 每次,使用base + offset副最小值/最大值索引,您可以搜索正确的一半数组

The purpose of having a method with a different argument set is to make the method easy to call for the user. 使用具有不同参数集的方法的目的是使该方法易于为用户调用。 This is very common in recursion. 这在递归中很常见。

There is the public method that is available to the user that has the 有一个公共方法可供具有该用户的用户使用
int bin(int value, int size, int array[]) signature. int bin(int value, int size, int array[])签名。

There should then be the private, internal helper method with the 那么应该有私有的,内部的帮助方法
int bin(int value, int max, int min, int array[]) signature. int bin(int value, int max, int min, int array[])签名。

The point is, someone calling this method will want to pass in the size of the array, and not the start and end index (because to them, the start index should always be 0, end should always be size-1) and it is bad practice to have a method with arguments that are preset like that. 关键是,有人调用此方法将要传递数组的大小,而不是传入的开始和结束索引(因为对它们来说,起始索引应始终为0,end应始终为size-1)并且它是不正确的做法,让一个方法具有预设的参数。

The helper method with the start and end values (min and max) is used in the recursive calls for more efficient computations, and it hides the unnecessary arguments from the user. 具有开始和结束值(min和max)的辅助方法在递归调用中用于更有效的计算,并且它隐藏了来自用户的不必要的参数。

Your method should then look something like this: 你的方法应该是这样的:

int bin(int value, int size, int array[]) {
    return bin(value, size - 1, 0, array);
}
int * binSearch (int *arr,int size, int num2Search)

{

    if(size==1)

        return ((*arr==num2Search)?(arr):(NULL));

    if(*(arr+(size/2))<num2Search)

        return binSearch(arr+(size/2)+1,(size/2),num2Search);

    if(*(arr+(size/2))==num2Search)

        return (arr+(size/2));

    return binSearch(arr,(size/2),num2Search);

}

in my function, you get the same parameters and return the adress of the value. 在我的函数中,您获得相同的参数并返回值的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM