简体   繁体   English

C语言中的二元搜索算法

[英]Binary chop search algorithm in C

I'm trying to write a binary chop search function in C and having some issues. 我正在尝试用C语言编写二进制印章搜索功能,并且遇到了一些问题。 First of all, after finding the midpoint values, the function isn't even entering any of the if loops. 首先,在找到中点值之后,该函数甚至没有进入任何if循环。

The function is this: 函数是这样的:

   int binarychopsearch(int i, int *array, int min, int N) {

min = 0;
int max = N - 1;
printf("Min = %d, Max = %d\n", min, max);
printf("i = %d\n", i);



        int mid = (min + max)/2;
        //printf("midpoint = %d\n", mid);
        printf("array[%d] = %d\n", mid, array[mid]);
  if (i < array[mid]) {
        printf("in this loop\n");
        printf("i = %d, array[mid] = %d\n", i, array[mid]);
        // key is in lower subset
        return binarychopsearch(i, array, min, mid - 1);
        }


  else if (i > array[mid]) { 
      printf("in the greater than loop\n");
      printf("i = %d, array[mid] = %d\n", i, array[mid]);
      return binarychopsearch(i, array, mid + 1, max);
      }

  else 

  //if (i = array[mid]) {

        return mid;
}

I'm not including the main where the input values come from as I think the issue is in this function. 我不包括输入值的主要来源,因为我认为问题出在此函数中。 It is compiling and running, but not entering the loops so not finding the location of the "i" value. 它正在编译并运行,但是没有进入循环,因此找不到“ i”值的位置。 I'm pretty stuck on this as I can't see where it's going wrong. 我很坚持这一点,因为我看不出问题出在哪里。

Any help is really appreciated! 任何帮助都非常感谢!

Thanks 谢谢

If i is larger (or equal) to array[mid] then you unconditionally return from the function. 如果i大于(或等于) array[mid]那么您将无条件地从函数返回。 Instead you then should check the next condition, and then if that is false as well you know you have found the value you were looking for. 取而代之的是,您应该检查下一个条件,然后如果该条件也是假的,那么您就知道已经找到了要查找的值。

So it should look something like 所以它看起来应该像

if (i < array[mid])
{
    ...
}
else if (i > array[mid])
{
    ...
}
else
    return mid;

There are other problems with your code as well. 您的代码还有其他问题。

Lets say you have the following array 假设您有以下数组

int array[] = { 1, 2, 3, 4, 5 };

and you are looking for the value 5 . 而您正在寻找值5

The calls will be like this: 调用将如下所示:

| Call# | min | N | max | mid | array[mid] |
|   1   |  0  | 5 |  4  |  2  |    3       |
|   2   |  3  | 4 |  3  |  3  |    4       |
|   3   |  4  | 3 |  2  |  2  |    3       |
|   4   |  3  | 2 |  1  |  2  |    3       |
|   5   |  3  | 1 |  0  |  1  |    2       |
|   6   |  2  | 0 | -1  |  1  |    2       |
.
.
.

It's quite clear that this will not end well. 很显然,这不会很好地结束。 In fact, you will soon end up with negative index, and that leaves you in the territory of undefined behavior . 实际上,您很快就会以索引结尾,这使您处于不确定行为的领域

I suggest you create your own table like this, on paper, when trying to fix your algorithm, for both cases of recursion. 对于两种递归情况,建议您在尝试修复算法时在纸上创建自己的表。

If you don't call the recursive function with max but with N instead, ie 如果您使用max而是使用N来调用递归函数,即

return binarychopsearch(i, array, mid + 1, N);

then you will have the following calls 那么您将有以下电话

| Call# | min | N | max | mid | array[mid] |
|   1   |  0  | 5 |  4  |  2  |    3       |
|   2   |  3  | 5 |  4  |  3  |    4       |
|   3   |  4  | 5 |  4  |  4  |    5       |

So the third call found the number, and returns the index 4 . 因此,第三个调用找到了该数字,并返回了索引4

You should also change the first call: 您还应该更改第一个电话:

return binarychopsearch(i, array, min, mid);

The problem is when i >= array[mid] : you systematically return -1: use esle if 问题是当i >= array[mid] :您系统地返回-1: esle if使用esle if

if(i < array[mid])
{}
else if(i > array[mid])
{}
else // i == array[mid]
{}

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

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