mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-03 18:01:09 +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]")
|
print("couldn't find root in [a,b]")
|
||||||
return
|
return
|
||||||
else:
|
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
|
while abs(start - mid) > 10**-7: # until we achieve precise equals to 10^-7
|
||||||
if function(mid) == 0:
|
if function(mid) == 0:
|
||||||
return mid
|
return mid
|
||||||
|
@ -22,7 +22,7 @@ def bisection(function, a, b): # finds where the function becomes 0 in [a,b] us
|
||||||
end = mid
|
end = mid
|
||||||
else:
|
else:
|
||||||
start = mid
|
start = mid
|
||||||
mid = (start + end) / 2
|
mid = start + (end - start) / 2.0
|
||||||
return mid
|
return mid
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ def binary_search(sorted_collection, item):
|
||||||
right = len(sorted_collection) - 1
|
right = len(sorted_collection) - 1
|
||||||
|
|
||||||
while left <= right:
|
while left <= right:
|
||||||
midpoint = (left + right) // 2
|
midpoint = left + (right - left) // 2
|
||||||
current_item = sorted_collection[midpoint]
|
current_item = sorted_collection[midpoint]
|
||||||
if current_item == item:
|
if current_item == item:
|
||||||
return midpoint
|
return midpoint
|
||||||
|
|
Loading…
Reference in New Issue
Block a user