mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
better implementation for midpoint (#914)
This commit is contained in:
parent
a212efee5b
commit
b7cff04574
|
@ -14,7 +14,7 @@ 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
|
||||
|
@ -22,7 +22,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
|||
end = mid
|
||||
else:
|
||||
start = mid
|
||||
mid = (start + end) / 2
|
||||
mid = start + (end - start) / 2.0
|
||||
return mid
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user