+ 1
Binary Search in c
This program is working fine for searching value belonging to left half of the array.But when we want to search value in right half of the array,the program is not working.I think there is a logical mistake but I am not able to figure out. https://code.sololearn.com/cltn76GLgd6J/?ref=app
8 ответов
+ 5
Not surprising as it can't be reached. = goes in 1st, < in 2nd, > in 3rd. Nothing left for 4th to catch.
+ 7
It is a binary search so max is 4 loops. Added count to prove it. Any other means will loop more.
+ 6
You could have issues on either side as some numbers could never be reached. Changed your line 10.
https://code.sololearn.com/cnMKUvY72CEU
+ 5
Updated mine to add message.
+ 5
Changed again to better code.
+ 1
The last statement is still not running. Suppose I enter number 11 then it is not showing the statement "the number is not present in the array"
+ 1
But if we use if(l==r && x[m] != y) then the while loop will be repeated 10 times.Is there any other way to detect it in first loop itself.
0
But still the loop need to be repeat 10 times before arriving at the conclusion that the number 11 is not present in the array.