- Off-by-one in loop condition: Use
left <= rightfor standard search,left < rightfor boundary finding. Getting this wrong causes infinite loops or missed elements. - Integer overflow in mid calculation: Use
mid = left + (right - left) / 2instead of(left + right) / 2to prevent overflow with large indices. - Wrong half elimination: When target could be at mid, don't exclude mid from the next search range.
Use right = mid not right = mid - 1 in boundary searches.