+ 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

15th Aug 2018, 9:08 PM
harshit
harshit - avatar
8 Respuestas
+ 5
Not surprising as it can't be reached. = goes in 1st, < in 2nd, > in 3rd. Nothing left for 4th to catch.
15th Aug 2018, 9:47 PM
John Wells
John Wells - avatar
+ 7
It is a binary search so max is 4 loops. Added count to prove it. Any other means will loop more.
16th Aug 2018, 1:53 AM
John Wells
John Wells - avatar
+ 6
You could have issues on either side as some numbers could never be reached. Changed your line 10. https://code.sololearn.com/cnMKUvY72CEU
15th Aug 2018, 9:30 PM
John Wells
John Wells - avatar
+ 5
Updated mine to add message.
15th Aug 2018, 9:49 PM
John Wells
John Wells - avatar
+ 5
Changed again to better code.
15th Aug 2018, 10:12 PM
John Wells
John Wells - avatar
+ 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"
15th Aug 2018, 9:42 PM
harshit
harshit - avatar
+ 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.
15th Aug 2018, 10:07 PM
harshit
harshit - avatar
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.
15th Aug 2018, 10:21 PM
harshit
harshit - avatar