A Problem in Many Binary Search Implementations?

We know that the binary search algorithm is better than the linear search algorithm. This algorithm takes O(log n) amount of time to execute. Though most of the cases the implemented code has some problem. Let us consider one binary search algorithm function like below −

Syntax

int binarySearch(int array[], int start, int end, int key);

Problem in Common Implementation

Here is a common but problematic implementation of binary search −

#include <stdio.h>

int binarySearch(int array[], int start, int end, int key){
    if(start <= end){
        int mid = (start + end) / 2; // This line has potential overflow problem
        if(array[mid] == key)
            return mid;
        if(array[mid] > key)
            return binarySearch(array, start, mid-1, key);
        return binarySearch(array, mid+1, end, key);
    }
    return -1;
}

int main() {
    int arr[] = {1, 3, 5, 7, 9, 11, 13, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 7;
    
    int result = binarySearch(arr, 0, n-1, key);
    if(result != -1)
        printf("Element found at index: %d<br>", result);
    else
        printf("Element not found<br>");
    
    return 0;
}
Element found at index: 3

This algorithm will work fine until the start and end reaches a large number. If the (start + end) is exceeding the value of 231 − 1 (for 32-bit integers) then it may return one negative number after integer overflow. And as negative numbers are not valid array indices, this may cause serious problems.

Method 1: Safe Addition Approach

The safest way to calculate the midpoint is to use the following formula −

#include <stdio.h>

int binarySearchSafe(int array[], int start, int end, int key){
    if(start <= end){
        int mid = start + ((end - start) / 2); // Prevents overflow
        if(array[mid] == key)
            return mid;
        if(array[mid] > key)
            return binarySearchSafe(array, start, mid-1, key);
        return binarySearchSafe(array, mid+1, end, key);
    }
    return -1;
}

int main() {
    int arr[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 12;
    
    int result = binarySearchSafe(arr, 0, n-1, key);
    if(result != -1)
        printf("Element found at index: %d<br>", result);
    else
        printf("Element not found<br>");
    
    return 0;
}
Element found at index: 5

Method 2: Using Bit Shifting with Casting

In C, we can use unsigned casting and bit shifting to avoid overflow −

#include <stdio.h>

int binarySearchBitShift(int array[], int start, int end, int key){
    if(start <= end){
        int mid = ((unsigned int)start + (unsigned int)end) >> 1;
        if(array[mid] == key)
            return mid;
        if(array[mid] > key)
            return binarySearchBitShift(array, start, mid-1, key);
        return binarySearchBitShift(array, mid+1, end, key);
    }
    return -1;
}

int main() {
    int arr[] = {5, 10, 15, 20, 25, 30, 35, 40};
    int n = sizeof(arr) / sizeof(arr[0]);
    int key = 25;
    
    int result = binarySearchBitShift(arr, 0, n-1, key);
    if(result != -1)
        printf("Element found at index: %d<br>", result);
    else
        printf("Element not found<br>");
    
    return 0;
}
Element found at index: 4

Comparison of Methods

Method Formula Advantages Disadvantages
Standard (Problematic) (start + end) / 2 Simple and intuitive Integer overflow for large indices
Safe Addition start + (end - start) / 2 No overflow, clear logic Slightly more complex
Bit Shifting ((unsigned)start + (unsigned)end) >> 1 Efficient, handles large values Less readable for beginners

Conclusion

The overflow problem in binary search is a critical issue when dealing with large arrays. The safe addition method start + (end - start) / 2 is the most recommended approach as it prevents overflow while maintaining code readability and works consistently across all platforms.

Updated on: 2026-03-15T10:57:02+05:30

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements