Skip to content

Commit 552c01f

Browse files
authored
Merge pull request #123 from brayo-pip/patch-1
Avoids Overflow Errors common in Binary search
2 parents 5059797 + dc9501d commit 552c01f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

searches/binary_search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ func binarySearch(array []int, target int, lowIndex int, highIndex int) int {
77
if highIndex < lowIndex {
88
return -1
99
}
10-
mid := int((lowIndex + highIndex) / 2)
10+
mid := int(lowIndex + (highIndex-lowIndex)/2)
1111
if array[mid] > target {
1212
return binarySearch(array, target, lowIndex, mid)
1313
} else if array[mid] < target {
@@ -22,7 +22,7 @@ func iterBinarySearch(array []int, target int, lowIndex int, highIndex int) int
2222
endIndex := highIndex
2323
var mid int
2424
for startIndex < endIndex {
25-
mid = int((startIndex + endIndex) / 2)
25+
mid = int(startIndex + (endIndex-startIndex))
2626
if array[mid] > target {
2727
endIndex = mid
2828
} else if array[mid] < target {

0 commit comments

Comments
 (0)