Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binary search middle value calculation

The following is the pseudocode I got from a TopCoder tutorial about binary search

binary_search(A, target):    lo = 1, hi = size(A)    while lo <= hi:       mid = lo + (hi-lo)/2       if A[mid] == target:          return mid                   else if A[mid] < target:           lo = mid+1       else:          hi = mid-1     // target was not found 

Why do we calculate the middle value as mid = lo + (hi - lo) / 2 ? Whats wrong with (hi + lo) / 2

I have a slight idea that it might be to prevent overflows but I'm not sure, perhaps someone can explain it to me and if there are other reasons behind this.

like image 357
binsearch Avatar asked Dec 26 '10 15:12

binsearch


People also ask

What is the purpose to calculate mid value in binary search?

The reason for using mid= low+(high-low)/2 is to avoid integer overflow.

Does binary search start in the middle?

Binary Search Algorithm: The basic steps to perform Binary Search are: Begin with the mid element of the whole array as a search key. If the value of the search key is equal to the item then return an index of the search key.

How do you find mid index?

Given a 0-indexed integer array nums , find the leftmost middleIndex (i.e., the smallest amongst all the possible ones). A middleIndex is an index where nums[0] + nums[1] + ... + nums[middleIndex-1] == nums[middleIndex+1] + nums[middleIndex+2] + ...


2 Answers

Yes, (hi + lo) / 2 may overflow. This was an actual bug in Java binary search implementation.

No, there are no other reasons for this.

like image 36
zeuxcg Avatar answered Oct 14 '22 10:10

zeuxcg


Although this question is 5 years old, but there is a great article in googleblog which explains the problem and the solution in detail which is worth to share.

It's needed to mention that in current implementation of binary search in Java mid = lo + (hi - lo) / 2 calculation is not used, instead the faster and more clear alternative is used with zero fill right shift operator

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

like image 94
vtor Avatar answered Oct 14 '22 11:10

vtor