Skip to content

Commit b7cff04

Browse files
wuminbinpoyea
authored andcommitted
better implementation for midpoint (TheAlgorithms#914)
1 parent a212efe commit b7cff04

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

arithmetic_analysis/bisection.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
1414
print("couldn't find root in [a,b]")
1515
return
1616
else:
17-
mid = (start + end) / 2
17+
mid = start + (end - start) / 2.0
1818
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
1919
if function(mid) == 0:
2020
return mid
2121
elif function(mid) * function(start) < 0:
2222
end = mid
2323
else:
2424
start = mid
25-
mid = (start + end) / 2
25+
mid = start + (end - start) / 2.0
2626
return mid
2727

2828

searches/binary_search.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def binary_search(sorted_collection, item):
4545
right = len(sorted_collection) - 1
4646

4747
while left <= right:
48-
midpoint = (left + right) // 2
48+
midpoint = left + (right - left) // 2
4949
current_item = sorted_collection[midpoint]
5050
if current_item == item:
5151
return midpoint

0 commit comments

Comments
 (0)