Binary Search conditions

Revision en1, by taap_cader, 2021-08-16 21:29:00

I know binary search but most of the time I gets confused how to find mid index and what to return low or high.
There are two while loop conditions:
1. while(low<high)
2. while(low<=high)
There are two ways to find mid element:
1. mid = (low+high)/2
2. mid = (low+high+1)/2
And then depending on the required condition we do low = mid+1 and right = mid or low = mid and right = mid-1 or something else. I know it's something keeping low always as our answer and returning that after while loop breaks and if high is always possible answer we return high. But I don't know clearly what to do when. Is there any logical way to do this stuff, I have try some combinations before getting right pair of conditions.
Thanks in advance.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English taap_cader 2021-08-16 21:29:00 792 Initial revision (published)