mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-09-28 13:33:11 +08:00
fix: sub-interval midpoint formula in ternary search (#15070)
* fix: sub-interval midpoint formula in ternary search * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
parent
f5988cc097
commit
acd4b34b0e
@@ -82,6 +82,11 @@ def ite_ternary_search(array: list[int], target: int) -> int:
|
||||
-1
|
||||
>>> ite_ternary_search([.1, .4 , -.1], .1)
|
||||
0
|
||||
>>> test_list_large = list(range(100))
|
||||
>>> ite_ternary_search(test_list_large, 65)
|
||||
65
|
||||
>>> ite_ternary_search(test_list_large, 105)
|
||||
-1
|
||||
"""
|
||||
|
||||
left = 0
|
||||
@@ -90,8 +95,8 @@ def ite_ternary_search(array: list[int], target: int) -> int:
|
||||
if right - left < precision:
|
||||
return lin_search(left, right, array, target)
|
||||
|
||||
one_third = (left + right) // 3 + 1
|
||||
two_third = 2 * (left + right) // 3 + 1
|
||||
one_third = left + (right - left) // 3
|
||||
two_third = right - (right - left) // 3
|
||||
|
||||
if array[one_third] == target:
|
||||
return one_third
|
||||
@@ -99,13 +104,13 @@ def ite_ternary_search(array: list[int], target: int) -> int:
|
||||
return two_third
|
||||
|
||||
elif target < array[one_third]:
|
||||
right = one_third - 1
|
||||
right = one_third
|
||||
elif array[two_third] < target:
|
||||
left = two_third + 1
|
||||
|
||||
else:
|
||||
left = one_third + 1
|
||||
right = two_third - 1
|
||||
right = two_third
|
||||
return -1
|
||||
|
||||
|
||||
@@ -133,12 +138,19 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
|
||||
-1
|
||||
>>> rec_ternary_search(0, 3, [.1, .4 , -.1], .1)
|
||||
0
|
||||
>>> test_list_large = list(range(100))
|
||||
>>> rec_ternary_search(0, len(test_list_large), test_list_large, 65)
|
||||
65
|
||||
>>> rec_ternary_search(20, 80, test_list_large, 65)
|
||||
65
|
||||
>>> rec_ternary_search(20, 80, test_list_large, 15)
|
||||
-1
|
||||
"""
|
||||
if left < right:
|
||||
if right - left < precision:
|
||||
return lin_search(left, right, array, target)
|
||||
one_third = (left + right) // 3 + 1
|
||||
two_third = 2 * (left + right) // 3 + 1
|
||||
one_third = left + (right - left) // 3
|
||||
two_third = right - (right - left) // 3
|
||||
|
||||
if array[one_third] == target:
|
||||
return one_third
|
||||
@@ -146,11 +158,11 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
|
||||
return two_third
|
||||
|
||||
elif target < array[one_third]:
|
||||
return rec_ternary_search(left, one_third - 1, array, target)
|
||||
return rec_ternary_search(left, one_third, array, target)
|
||||
elif array[two_third] < target:
|
||||
return rec_ternary_search(two_third + 1, right, array, target)
|
||||
else:
|
||||
return rec_ternary_search(one_third + 1, two_third - 1, array, target)
|
||||
return rec_ternary_search(one_third + 1, two_third, array, target)
|
||||
else:
|
||||
return -1
|
||||
|
||||
@@ -165,7 +177,7 @@ if __name__ == "__main__":
|
||||
assert collection == sorted(collection), f"List must be ordered.\n{collection}."
|
||||
target = int(input("Enter the number to be found in the list:\n").strip())
|
||||
result1 = ite_ternary_search(collection, target)
|
||||
result2 = rec_ternary_search(0, len(collection) - 1, collection, target)
|
||||
result2 = rec_ternary_search(0, len(collection), collection, target)
|
||||
if result2 != -1:
|
||||
print(f"Iterative search: {target} found at positions: {result1}")
|
||||
print(f"Recursive search: {target} found at positions: {result2}")
|
||||
|
||||
Reference in New Issue
Block a user