The most common method to calculate mid or middle element index in Binary Search Algorithm is to find the middle of the highest index and lowest index of the searchable space, using the formula mid = low + \frac{(high – low)}{2}

Finding the middle index “mid” in Binary Search Algorithm
Is this method to find mid always correct in Binary Search?Â
Consider the following implementation of the Binary Search function:
C
int binarySearch( int arr[], int low, int high, int x)
{
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] <= x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
|
Java
public static int binarySearch( int [] arr, int low, int high, int x) {
while (low <= high) {
int mid = (low + high) / 2 ;
if (arr[mid] == x)
return mid;
if (arr[mid] < x)
low = mid + 1 ;
else
high = mid - 1 ;
}
return - 1 ;
}
|
Python3
def binary_search(arr, low, high, x):
while low < = high:
mid = (low + high) / / 2
if arr[mid] = = x:
return mid
elif arr[mid] < x:
low = mid + 1
else :
high = mid - 1
return - 1
arr = [ 2 , 3 , 4 , 10 , 40 ]
x = 10
result = binary_search(arr, 0 , len (arr) - 1 , x)
if result ! = - 1 :
print (f "Element {x} is present at index {result}" )
else :
print (f "Element {x} is not present in the array" )
|
C#
using System;
class Program
{
static int BinarySearch( int [] arr, int low, int high, int x)
{
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
else if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
static void Main()
{
int [] arr = { 2, 3, 4, 10, 40 };
int x = 10;
int result = BinarySearch(arr, 0, arr.Length - 1, x);
if (result != -1)
Console.WriteLine($ "Element {x} is present at index {result}" );
else
Console.WriteLine($ "Element {x} is not present in the array" );
}
}
|
Javascript
function binarySearch(arr, low, high, x) {
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === x)
return mid;
if (arr[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
|
The above code looks fine except for one subtle thing, the expressionÂ
mid = (low + high)/2.
It fails for large values of low and high. Specifically, it fails if the sum of low and high is greater than the maximum positive value of int data type (i.e., 231 – 1). The sum overflows to a negative value, and the value stays negative when divided by two. This causes an array index out of bounds with unpredictable results.Â
What is the correct way to calculate “mid” in Binary Search Algorithm?
The 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Â
A similar problem appears in other similar types of divide and conquer algorithms like Merge Sort as well. The above problem occurs when values of low and high are such that their sum is greater than the permissible limit of the data type. Although, this much size of an array is not likely to appear most of the time.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Dec, 2023
Like Article
Save Article