Pages

Jul 13, 2015

Be careful of implementing "binary search"

When using a binary search, we usually will calculate the middle of the search space by
mid = (start + end) / 2

However, if implement this calculation on a computer program, it might cause an overflow problem if the start and end are already too large. Therefore, instead of using (start + end) / 2, we could use
mid = start + (end - start) / 2


See more detail: http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html

No comments:

Post a Comment