【问题标题】:How to return NULL if binary searched number is not in index?如果二进制搜索的数字不在索引中,如何返回 NULL?
【发布时间】:2019-06-24 11:13:45
【问题描述】:

我正在编写一个代码,它基本上完成了一个主要问题。我写了一个二分搜索函数,返回找到的索引。每当我运行我的代码并搜索 2 的任何幂时,它都能正常工作。但是,每当我输入任何其他数字(例如 50)时,它都会返回错误。

在我的代码末尾,我有一个 else 语句,说明如果其他语句都没有返回值来返回 NULL,所以我遇到了一些麻烦。谢谢。我在 Xcode 和 UNIX 服务器上运行,但我注释掉了在 UNIX 服务器上运行的行。

#include <stdio.h>
#include <stdlib.h>
    int* search(int* begin, int* end, int needle);
    int main(int argc, char **argv) { //int argc = 1, char **argv array of char pointers
        int num = 0;
        int nums[10], i;
        int *found = NULL;
        if(argc != 2) {
            printf("Enter a number to a power of 2 to search for:\n");
            scanf("%d" , &num);
        }
       // num = atoi(argv[1]);
        for(i = 0; i < 10; i++) { // initialzes array by shifting binary code to the left adding powers of 2
            nums[i] = 1 << i; }
        found = search(nums, &nums[9], num);
        if(found) {
            printf("Number %d found in index %ld.\n", num, found - nums);
                 }
        else {
            printf("Number %d was not found.\n", num);
       }
        return 0;
    }
int* search(int* begin, int* end, int needle){
    int *middle = (end-begin)/2 + begin;
    if(*middle == needle){
        return middle;
    }
    else if(needle < *middle){
        end = middle;
        return search(begin, end-1, needle);
    }
    else if(needle > *middle)
    {
        begin = middle;
        return search(begin+1, end, needle);
    }
    else
        return NULL;
}

我希望 main() 函数中的 else 语句在搜索到的值不在索引中时执行。

【问题讨论】:

    标签: c algorithm pointers search binary


    【解决方案1】:

    您的问题是递归没有基本案例

    这样想:如果数字不在数组中,你的针总是比中间大或小

    意味着它永远不会到达最后一个 else,它总是会递归递增或递减,直到它尝试取消引用乱码,因此出现分段错误

    您需要将旧的良好基本情况添加到您的二进制搜索中,如下所示:

    int* search(int* begin, int* end, int needle) {
        int *middle = (end-begin)/2 + begin;
    
        if(begin == end) {
            //recursion ends when there are no more segments to divide in two
            //so after your final single element segment, a decrement or increment will happen
            //making your end and begin pointers the same 
            return NULL;
        }
        else if(*middle == needle) {
            return middle;
        }
        else if(needle < *middle) {
            end = middle;
            return search(begin, end-1, needle);
        }
        else if(needle > *middle) {
            begin = middle;
            return search(begin+1, end, needle);
        }   
    }
    

    【讨论】:

    • 这段代码实际上不起作用。但是,您给了我洞察力,以找出可以解决的代码。如果我使用您的 if 语句:if(begin == end),我没有检查正确的索引字段。我不得不使用这个 if 语句来执行我的代码: if(begin+1 == end-1) return NULL;也许你可以进一步解释为什么?另外,我的第一个代码是错误的。在第二个 else if 中,它应该是 begin -1,第二个 else if 应该是 end+1。感谢您的意见。
    • 代码确实工作。我自己编译并测试过。不,您不需要添加开始 -1 和结束 +1。另外,我确实解释了原因
    猜你喜欢
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2020-04-02
    相关资源
    最近更新 更多