Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6150315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:35:52+00:00 2026-05-23T19:35:52+00:00

I was reading an algorithms book which had the following algorithm for binary search:

  • 0

I was reading an algorithms book which had the following algorithm for binary search:

public class BinSearch {
  static int search ( int [ ] A, int K ) {
    int l = 0 ;
    int u = A. length −1;
    int m;
    while (l <= u ) {
      m = (l+u) /2;
      if (A[m] < K) {
        l = m + 1 ;
      } else if (A[m] == K) {
        return m;
        } else {
          u = m−1;
        }
       }
       return −1;
      }
 }

The author says “The error is in the assignment m = (l+u)/2; it can lead to overflow and should be replaced by m = l + (u-l)/2.”

I can’t see how that would cause an overflow. When I run the algorithm in my mind for a few different inputs, I don’t see the mid’s value going out of the array index.

So, in which cases would the overflow occur?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:35:53+00:00Added an answer on May 23, 2026 at 7:35 pm

    This post covers this famous bug in a lot of detail. As others have said it’s an overflow issue. The fix recommended on the link is as follows:

    int mid = low + ((high - low) / 2);
    
    // Alternatively
    int mid = (low + high) >>> 1;
    

    It is also probably worth mentioning that in case negative indices are allowed, or perhaps it’s not even an array that’s being searched (for example, searching for a value in some integer range satisfying some condition), the code above may not be correct as well. In this case, something as ugly as

    (low < 0 && high > 0) ? (low + high) / 2 : low + (high - low) / 2
    

    may be necessary. One good example is searching for the median in an unsorted array without modifying it or using additional space by simply performing a binary search on the whole Integer.MIN_VALUE–Integer.MAX_VALUE range.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading the Cormen algorithms book (binary search tree chapter) and it says that
I am reading about Dynamic programming in Cormen etc book on algorithms. following is
I am reading a algorithms book by S.DasGupta. Following is text snippet from the
I'm reading about STL algorithms and the book pointed out that algorithms like find
I am reading Nicolai Josuttis book on C++STL algorithms. For many algorithms such as
I have question that comes from a algorithms book I'm reading and I am
I'm reading my algorithms text book, and I'm reading about recurrence relations and finding
Reading a book on algorithms. Can someone explain the meaning of the mathematical symbol
I've just now started reading an Algorithms book that defined Graphs as follows: Graphs
I'm reading a algorithm book in my lesure time. Here is a question I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.