Binary search: Difference between revisions
Content deleted Content added
No edit summary |
→{{header|Java}}: changed calculation of average - adding lo + hi, then dividing by two causes an overflow error with numbers close to Integer.MAX_VALUE |
||
Line 1,104: | Line 1,104: | ||
int lo = 0; |
int lo = 0; |
||
while(hi >= lo){ |
while(hi >= lo){ |
||
guess = (hi |
guess = lo + ((hi - lo) / 2); |
||
if(nums[guess] > check){ |
if(nums[guess] > check){ |
||
hi = guess - 1; |
hi = guess - 1; |