On Sun, Oct 08, 2017 at 02:29:37PM -0400, Derrick Stolee wrote:
> A common mistake when writing binary search is to allow possible
> integer overflow by using the simple average:
>
> mid = (min + max) / 2;
>
> Instead, use the overflow-safe version:
>
> mid = min + (max - min) / 2;
A common mistake when writing binary search is to allow possible
integer overflow by using the simple average:
mid = (min + max) / 2;
Instead, use the overflow-safe version:
mid = min + (max - min) / 2;
This translation is safe since the operation occurs inside a loop
2 matches
Mail list logo