0

I want to implement binary search for an array of integers. I made sure that array sorted incrementally. But my function is not tracking all the cases and some of the checks fail. What am I missing here? Value stands for integer I'm trying to find.

bool search(int value, int array[], int n)
{   
    int left = 0;
    int right = n - 1;
    int middle = (left + right) / 2;

while (right >= left)
{
    if (array[middle] == value)
    return true;
    else if (array[middle] < value)
    right = middle;
    else if (array[middle] > value)
    left = middle + 1;

    middle = (left + right) / 2;
}

return false;
} 

My guess is that some left or right border cases are not predicted. I also not strong about while condition.

6
  • 2
    You really should learn how to use a debugger. Commented Jan 27, 2017 at 15:39
  • Debugger of use printf - debugging. Output of left, middle, right for a failing example should lead to the problem. Commented Jan 27, 2017 at 15:44
  • 4
    How do you implement binary search? Carefully. It's been observed that the first implementation of binary search was published in 1946, but the first correct implementation of binary search was published in 1960. You'll learn a lot by debugging your own version, and making sure it works on all cases. Commented Jan 27, 2017 at 15:46
  • @MrSmith42 That's good idea, I'll try this out. Commented Jan 27, 2017 at 15:50
  • 3
    @SteveSummit Good point. I believe the typical pitfall is integer overflow in the middle computation, which is easily missed in human-scaled test cases. Wikipedia has notes on this. The recommended expression is middle = left + (right - left) / 2;. Commented Jan 27, 2017 at 15:50

2 Answers 2

6

You mixed left and right, if array[middle] < value you have to change left.

bool search(int value, int array[], int n)
{
    int left = 0;
    int right = n - 1;
    int middle = left + (right - left) / 2;

    while (right >= left)
    {
        if (array[middle] == value)
            return true;
        else if (array[middle] > value)
            right = middle - 1;
        else if (array[middle] < value)
            left = middle + 1;

        middle = (left + right) / 2;
    }
    return false;
}

will work: http://ideone.com/VSAhnZ

Further improvements can be:

  1. You can exclude middle in the else parts, because you have already proved that it is not the correct value (right = middle; => right = middle - 1;)

  2. The second else if can be replaced by an else. If it is not the value and not smaller you do not have to test if it is larger.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it. Your answer helped me a lot, big thanks. Can't believe I couldn't figure out this kind of mixing for such a long time. I'll try to be more attentive in the future.
-1

Standard binary search algorithm can be found anywhere. but just correcting your code:

bool search(int value, int array[], int n)
{   
    int left = 0;
    int right = n - 1;
    int middle;
    while (left <= right)
    {
         middle = (left + right) / 2;    
        if (value > array[middle] )
            left = middle + 1;
        else if (value < array[middle])
            right = middle - 1;                
        else 
            return true;
     }
     return false;
} 

Comments

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.