0

enter image description here

i don't understand acc. to the algorithm it should work correctly , but it doesn't

0

Binary search can be tricky to implement. Take a day off, when you come back read what the code does out load, while drafting on paper exactly what it does.

One thing I am picking up on here though is that your code won't find 42 if it is the last number to the right. Then this is probably an off-by-one error of sorts, so I think (likely wrong) that you should remove the +1 in low = mid +1 statement at line 34 in your picture.

On the side, structuring your code clearly tremendously help your efforts in catching bugs, much more than clumping everything together at least. Whitespace is your friend, add some of that. Also, in the else if at line 33 through 35, your curly braces don't embrace all expressions and there is no indentation. Sample fix below.

else if (values > values[mid])
{
    low = mid + 1;
    mid = (low + high) / 2;
}
| improve this answer | |
  • tried that didn't work and when i try to do it maunually it should be done , i even put a condition above while that: if(value==values[high] { return true;) – Ketan Verma Sep 25 '16 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .