Consider the following C implementation of Binary Search function, is there anything wrong in this?

[pastacode lang=”c” manual=”%2F%2F%20A%20iterative%20binary%20search%20function.%20It%20returns%20location%20of%20x%20in%0A%2F%2F%20given%20array%20arr%5Bl..r%5D%20if%20present%2C%20otherwise%20-1%0Aint%20binarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%7B%0A%20%20%20%20while%20(l%20%3C%3D%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20find%20index%20of%20middle%20element%0A%20%20%20%20%20%20%20%20int%20m%20%3D%20(l%2Br)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20x%20is%20present%20at%20mid%0A%20%20%20%20%20%20%20%20if%20(arr%5Bm%5D%20%3D%3D%20x)%20return%20m%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20greater%2C%20ignore%20left%20half%0A%20%20%20%20%20%20%20%20if%20(arr%5Bm%5D%20%3C%20x)%20l%20%3D%20m%20%2B%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20smaller%2C%20ignore%20right%20half%0A%20%20%20%20%20%20%20%20else%20r%20%3D%20m%20-%201%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20if%20we%20reach%20here%2C%20then%20element%20was%20not%20present%0A%20%20%20%20return%20-1%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

The above looks fine except one subtle thing, the expression “m = (l+r)/2”. It fails for large values of l and r. Specifically, it fails if the sum of low and high is greater than the maximum positive int value (231– 1). The sum overflows to a negative value, and the value stays negative when divided by two. In C this causes an array index out of bounds with unpredictable results.

[ad type=”banner”]

What is the way to resolve this problem?
Following is one way:

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

Probably faster, and arguably as clear is (works only in Java, refer this):

        int mid = (low + high) >>> 1;

In C and C++ (where you don’t have the >>> operator), you can do this:

        mid = ((unsigned int)low + (unsigned int)high)) >> 1

The similar problem appears in Merge Sort as well.

The above content is taken from google reasearch blog.

Please refer this as well, it points out that the above solutions may not always work.

The above problem occurs when array length is 230 or greater and the search repeatedly moves to second half of the array. This much size of array is not likely to appear most of the time. For example, when we try the below program with 32 bit Code Blocks compiler, we get compiler error.

[ad type=”banner”] [pastacode lang=”c” manual=”int%20main()%0A%7B%0A%20%20%20%20int%20arr%5B1%3C%3C30%5D%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

Output:

error: size of array 'arr' is too large

Even when we try boolean array, the program compiles fine, but crashes when run in Windows 7.0 and Code Blocks 32 bit compiler

[pastacode lang=”c” manual=”%23include%20%3Cstdbool.h%3E%0Aint%20main()%0A%7B%0A%20%20%20%20bool%20arr%5B1%3C%3C30%5D%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

Output:

No compiler error, but crashes at run time.

[ad type=”banner”]