简体   繁体   中英

Binary chop search algorithm in C

I'm trying to write a binary chop search function in C and having some issues. First of all, after finding the midpoint values, the function isn't even entering any of the if loops.

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'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. 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 .

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

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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