Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 这个二进制搜索实现中的无限循环?_C_Binary Search - Fatal编程技术网

C 这个二进制搜索实现中的无限循环?

C 这个二进制搜索实现中的无限循环?,c,binary-search,C,Binary Search,有人能帮我找到这个二进制搜索算法实现中的错误吗。我猜它是在一个无限循环中运行的。函数定义中存在问题,但不确定是什么 #include <stdio.h> #define max 5 int binarysearch(int a[],int element,int first,int last);//prototype int main(void) { int arr[max]={1,2,3,7,8}; int b; int start=0; sc

有人能帮我找到这个二进制搜索算法实现中的错误吗。我猜它是在一个无限循环中运行的。函数定义中存在问题,但不确定是什么

#include <stdio.h>
#define max 5

int binarysearch(int a[],int element,int first,int last);//prototype

int main(void) 
{
    int arr[max]={1,2,3,7,8};
    int b;
    int start=0;
    scanf("%d",&b);
    int search=binarysearch(arr,b,start,max-1);
    if(search==-1)
    {
        puts("element is not there in array");
    }
    else
    {
        printf("element found at position %d",search);
    }
}

int binarysearch(int a[],int element,int first,int last)//definition
{
    int mid=(first+last)/2;
    int initial;int final;
    while(first<=last)
    {
        if(a[mid]==element)
        {
            return mid;
        }
        else if(a[mid]<element)
        {
            initial=mid+1;
            final=last;
            binarysearch(a,element,initial,final);
        }
        else if(a[mid]>element)
        {
            initial=first;
            final=mid-1;
            binarysearch(a,element,initial,final);
        }
    }
    return -1;
}
#包括
#定义最大值5
int二进制搜索(int a[],int元素,int first,int last)//原型
内部主(空)
{
int-arr[max]={1,2,3,7,8};
int b;
int start=0;
scanf(“%d”和“b”);
int search=binarysearch(arr,b,start,max-1);
如果(搜索==-1)
{
puts(“数组中不存在元素”);
}
其他的
{
printf(“在位置%d处找到元素”,搜索);
}
}
int binarysearch(int a[],int元素,int first,int last)//定义
{
int mid=(第一个+最后一个)/2;
int首字母;int末字母;

而(首先函数中有一个不必要的循环:

while(first<=last)
while(首先进行以下更改

int binarysearch(int a[],int element,int first,int last)//definition
{
int mid=(first+last)/2;
int initial;int final;

if(first > last)
{
  printf("Not found\n");
  return -1;
}
    if(a[mid]==element)
    {
        return mid;
    }
    else if(a[mid]<element)
    {
        initial=mid+1;
        final=last;
        binarysearch(a,element,initial,final);
    }
    else if(a[mid]>element)
    {
        initial=first;
        final=mid-1;
        binarysearch(a,element,initial,final);
    }
}
int-binarysearch(int a[],int元素,int-first,int-last)//定义
{
int mid=(第一个+最后一个)/2;
int首字母;int末字母;
如果(第一个>最后一个)
{
printf(“未找到”);
返回-1;
}
if(a[mid]==元素)
{
中途返回;
}
else if(一个[mid]元素)
{
初始=第一;
最终结果=中1;
二进制搜索(a、元素、首字母、最终字母);
}
}

不起作用:递归调用的返回值丢失。看起来这是一个家庭作业。请讨论您的问题,而不是试图给出现成的答案。Cheechanging while to if给出了一个运行时错误,但给出了正确的输出,但您建议的第二种方法有效。您能告诉我为什么会这样吗?我在ideone上试用过。com@noviceWhich错误?您是否添加了
返回
s?如果您有其他问题,请用新代码发布新问题。