【问题标题】:Compiler is skipping the return line. Binary Search by Recursion编译器正在跳过返回行。递归二分查找
【发布时间】:2015-05-17 17:06:43
【问题描述】:

我使用递归调用创建了一个二分搜索程序(忽略 printf 语句)。运行程序后,您会发现它运行良好并获取要找到的元素的索引,但在:

else if (element == arr[mid])
    {
        printf("\n%d\nmid = %d\nstart = %d\nend = %d\n\n", i, mid,start,end);
        return mid;
    }

它不返回中间值,而是返回末尾的 0。 (我尝试过 0 以外的值)。

我也尝试在最后返回 mid 而不是返回 0。它总是返回 4。

整个代码是:

#include <stdio.h>

int arr[100]={0,1,2,3,4,5,6,7,8,9}, start=0, end=9, i=0;

int BinaryRecursivSearch(int element, int start, int end)
{
    int mid = (start + end)/2;

    i++;

    printf("\n%d\nmid = %d\nstart = %d\nend = %d\n\n", i, mid,start,end);

    if (mid == start)
    {
        if(element!=end)
        {
            printf("\nElement not found");
            return -1;
        }
    }

    else if (element < arr[mid])
        BinaryRecursivSearch(element, start, mid);

    else if (element > arr[mid])
        BinaryRecursivSearch(element, mid, end);

    else if (element == arr[mid])
    {

//For the last iteration (or recursion) mid has the correct value in printf

printf("\n%d\nmid = %d\nstart = %d\nend = %d\n\n", i, mid,start,end);
        return mid;  //It skips this
    }

    return 0;  //It returns this value
}

int main()
{
    int element, index;

    printf("Enter the element to be searched for: ");
    scanf("%d", &element);

    index = BinaryRecursivSearch(element, start, end);

    printf("\n Element found at %d position", index + 1 );

    return 0;

}

【问题讨论】:

  • 编译器跳过返回行,你真的不应该责怪编译器,很可能是你的错。
  • 请注意,您的最后一个代码块以else 开头,也许这就是它被跳过的原因。我建议您更改两个相同的 printf 消息之一,以便您知道正在打印哪一个。
  • 递归时需要return BinaryRecursivSearch();
  • 感谢您的快速回复代码到达返回中;我通过更改它上面的 printf 语句来检查它如果你
  • @Prayansh Srivastava 看来您的功能没有任何意义> 有 n0 需要调查它。你应该重写它。

标签: c recursion binary-search


【解决方案1】:

函数返回0,因为这些语句

BinaryRecursivSearch(element, start, mid);
...
BinaryRecursivSearch(element, mid, end);

应该是

return BinaryRecursivSearch(element, start, mid);
...
return BinaryRecursivSearch(element, mid, end);

因为它们不返回任何东西,所以执行最后的语句

return 0;

【讨论】:

  • 非常感谢老兄!没想到
【解决方案2】:

我认为你的功能没有任何意义。 比如我完全不明白这段代码sn-p是什么意思

if (mid == start)
{
    if(element!=end)
    {
        printf("\nElement not found");
        return -1;
    }
}

请注意,数组可以包含任何值,而不仅仅是您的程序中的 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }。因此,这段代码 sn-p 对二进制搜索方法没有任何意义。尝试使用不同值的数组,您的程序将无法按预期运行。

或者在这段代码中sn-p

else if (element

else if (element > arr[mid]) BinaryRecursivSearch(element, mid, end);

您没有使用 BinaryRecursivSearch 函数返回的值。

我建议把递归函数写得更简单

这是一个演示程序,展示了如何编写函数

#include <stdio.h>

int binary_search( const int a[], int n, int value )
{
    int middle;

    if ( n == 0 ) return -1;

    middle = n / 2;

    if ( value < a[middle] )
    {
        return binary_search( a, middle, value );
    }
    else if ( a[middle] < value )
    {
        int index = binary_search( a + middle + 1, n - middle - 1, value );
        return index == -1 ? -1 : index + middle + 1;
    }

    return middle;
}

#define N   10

int main(void) 
{
    int a[N] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for ( int i = 0; i < N; i++ ) printf( "%d ", a[i] );
    printf( "\n" );

    for ( int i = -1; i <= N; i++ )
    {
        printf( "%d has index %d\n", i, binary_search( a, N, i ) );
    }

    return 0;
}

程序输出是

0 1 2 3 4 5 6 7 8 9 
-1 has index -1
0 has index 0
1 has index 1
2 has index 2
3 has index 3
4 has index 4
5 has index 5
6 has index 6
7 has index 7
8 has index 8
9 has index 9
10 has index -1

【讨论】:

    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2020-11-13
    • 2021-04-08
    相关资源
    最近更新 更多