二进制搜索找不到正确的值

时间:2019-11-02 04:06:09

标签: c binary-search

我创建了一个包含1000个元素的向量,这些元素的值就是索引本身 V [100] = 100,V [50] = 50等等。

当我通过值调用二进制搜索时,它必须返回给我索引,它本身就是什么 因此,binary_search(vector,begin,end,50)必须返回50,但返回30。我尝试使用gdb进行调试,但找不到任何错误。

代码:

int rbb(int *v, int left, int right, int val) 
{

    int mid = (left + right) / 2;  //middle element

    if (right < left) //stop codition, pointers shifted
        return -1;

    if (val == v[mid]) //found value
        return mid;  

    if (val > v[mid]) //value is on vector right portion
        rbb(v, mid+1, right, val);

    if (val < v[mid]) //value is on vector left portion
        rbb(v, left, mid-1, val);

}

int main () 
{

    int v[1000];

    for (int i = 0; i < 1000; i++)
        v[i] = i;

    int x = rbb(v, 0, 999, 300);
    printf("%d", x);
}

4 个答案:

答案 0 :(得分:3)

您的代码存在的问题是,如果使用rbb函数,则不会从递归调用中返回值。您应该修改代码

if (val > v[mid]) //value is on vector right portion
    return rbb(v, mid+1, right, val);

if (val < v[mid]) //value is on vector left portion
    return rbb(v, left, mid-1, val);

答案 1 :(得分:-1)

您可以尝试

int rbb(int *v, int left, int right, int val) {

int mid = (left + right) / 2;  //middle element

if (right < left) //stop codition, pointers shifted
    return -1;

èlse if (val == v[mid]) //found value
    return mid;  

else if (val > v[mid]) //value is on vector right portion
    rbb(v, mid+1, right, val);

else if (val < v[mid]) //value is on vector left portion
    rbb(v, left, mid-1, val);
}


int main () {

int v[1000];

for (int i = 0; i < 1000; i++)
    v[i] = i;

int x = rbb(v, 0, 999, 300);
printf("%d", x);
}

答案 2 :(得分:-1)

问题出在返回中。当函数返回时,它不会结束,如果语句更改了中间变量,则继续调用其他函数。您可以这样更改


    int rbb (int *v, int left, int right, int val){
        int mid = (left + right) / 2;  //middle element

        if (right < left) //stop codition, pointers shifted
            return -1;

        else if (val == v[mid]) //found value
            return mid;

        else if (val > v[mid]) //value is on vector right portion
            rbb(v, mid+1, right, val);

        else if (val < v[mid]) //value is on vector left portion
            rbb(v, left, mid-1, val);
    }

    int main() {
        int v[1000];

        for (int i = 0; i < 1000; i++)
            v[i] = i;

        int x = rbb(v, 0, 999, 300);
        printf("%d", x);
    }

答案 3 :(得分:-2)

U可以使用此代码,您的代码也是正确的,但您应该使用  否则在比较时使用if语句...

  

度过了美好的一天   享受编码

#include<stdio.h>
int rbb(int v[], int left, int right, int val);
int main ()
{
int i,v[1000],x;
   for (i = 0; i <1000; i++)
     {
        v[i] = i;
     }
     x = rbb(v, 0,999, 300);
     printf("%d", x);

}
int rbb(int v[], int left, int right, int val) {
int mid;
mid = (left + right) / 2;  //middle element
if (right < left) //stop codition, pointers shifted
   {
      return 1;
   }
else if (val == v[mid]) //found value
   {    
      return mid;
   }
else if (val > v[mid]) //value is on vector right portion
    {
        rbb(v, mid+1, right, val);
    }
else//value is on vector left portion
    {
        rbb(v, left, mid-1, val);
    }

}