Skip to content

Commit

Permalink
better implementation for midpoint (TheAlgorithms#914)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuminbin authored and poyea committed Jun 24, 2019
1 parent a212efe commit b7cff04
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions arithmetic_analysis/bisection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
print("couldn't find root in [a,b]")
return
else:
mid = (start + end) / 2
mid = start + (end - start) / 2.0
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
if function(mid) == 0:
return mid
elif function(mid) * function(start) < 0:
end = mid
else:
start = mid
mid = (start + end) / 2
mid = start + (end - start) / 2.0
return mid


Expand Down
2 changes: 1 addition & 1 deletion searches/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def binary_search(sorted_collection, item):
right = len(sorted_collection) - 1

while left <= right:
midpoint = (left + right) // 2
midpoint = left + (right - left) // 2
current_item = sorted_collection[midpoint]
if current_item == item:
return midpoint
Expand Down

0 comments on commit b7cff04

Please sign in to comment.