DEV Community

Sanket Mohta
Sanket Mohta

Posted on

Binary search Loop condition

  1. while (low < high) is used when you're searching the range [low, high). When updating high, use high = mid. When updating low, use low = mid + 1.
  2. while (high - low > 1) is used when you're searching the range (low, high)





This does solve a lot of Doubts, and must be sincerely mentioned in All DSA sites when mentioning binary search

The condition of the loop in binary search should be according to the needs of the search and we get a result like that

To understand it, think of it in a way , what's the smallest input or boundary conditions that a search segment can have for which you dont need any further iterations/recrusive calls.

Top comments (0)