In binary search, we use mid = low + (high – low)/2
instead of (low + high)/2
to avoid overflow, however, can't calculate low/2
and high/2
separately and then sum them up rather than low+(( high-low)/2)
?
P.S. If low + (high – low)/2
is more efficient, then why is it so?
Let's say both low and high are 3; then middle = 3/2 + 3/2 = 1+1 = 2, which actually is quite bad. :-)
The reason we don't use middle=high/2+low/2 is that it gives us the wrong result