0

I coded a simple binary search to print out the position of what was searched. It correctly recognizes when a searched element isn't in the array and prints out "error". However, when a searched element is actually in the array, the value is printed, rather than the position. Could you please let me know what i'm missing? Thanks in advance

#include <stdio.h>
#include <stdlib.h>

int binarysearch(int array[], int low, int max, int search);
int main(void) {
    int array[10]={10,11,12,13,14,15,16,17,18,19};

    int count,search;
    count=sizeof(array)/sizeof(array[0]);

    printf("Enter the number you would like to search\n");
    scanf("%d",&search);

    int result=binarysearch(array,0,count,search);

    if (result>0){
        printf("Element in position %d",result);
    }
    else{
        printf("Error");
    }

}

int binarysearch(int array[], int low, int max, int search){
    if(low<=max){
        int middle=(low+max)/2;

        if(search>array[middle]){
            low=middle+1;
            return binarysearch(array,low,max,search);
        }

        else if(search<array[middle]){
            max=middle-1;
            return binarysearch(array,low,max,search);

        }

        else {
            return search;
        }

    }
    else
    return -1;
}
2
  • 5
    return middle maybe ? Commented Dec 17, 2017 at 7:03
  • 1
    Shouldn't you be returning middle? Commented Dec 17, 2017 at 7:03

1 Answer 1

2

Apart from the return middle; (in place of return search) change you should also change the

if (result>=0){
         ^^^^
        printf("Element in position %d",result);
    }

Otherwise you will always miss the 0-th element even if found. (By miss I mean the result won't be printed). Binary search will work as intended.

One another thing to notice is while calculating mid to avoid overflow use this

int middle = low + ((max - low) / 2);

Finally, the use of recursion is purely wasteful.

int binarysearch(int array[], int low, int max, int search) {
    while (low <= max) {
        int middle = low + ((max - low) / 2);
        if (search > array[middle]) {
            low = middle + 1;
        }
        else if (search < array[middle]) {
            max = middle - 1;
        }
        else {
            return middle;
        }
    }

    return -1;
}
Sign up to request clarification or add additional context in comments.

4 Comments

@ikegami.: Thanks for the awesome addition. Appreciate your help.
Thanks for the answers. Is it best to avoid using a recursion when possible, would it be better memory-wise?
@Roomi.: Yes reursion is limited by the amount of memory the system allows you to use when storing the stack frames due to each function call. Iteration avoids that. So yes it's preferrable to use iterative soluton.
It's also more expensive to push the arguments on the stack and make the call than to simply jump back to the start of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.