2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2017-10-09 02:19:39 +00:00
|
|
|
This is a type of divide and conquer algorithm which divides the search space into
|
2019-08-19 13:37:49 +00:00
|
|
|
3 parts and finds the target value based on the property of the array or list
|
2017-10-09 02:19:39 +00:00
|
|
|
(usually monotonic property).
|
|
|
|
|
|
|
|
Time Complexity : O(log3 N)
|
|
|
|
Space Complexity : O(1)
|
2019-10-05 05:14:13 +00:00
|
|
|
"""
|
2017-10-09 02:19:39 +00:00
|
|
|
import sys
|
2020-10-05 17:44:35 +00:00
|
|
|
from typing import List
|
2017-10-09 02:19:39 +00:00
|
|
|
|
|
|
|
# This is the precision for this function which can be altered.
|
|
|
|
# It is recommended for users to keep this number greater than or equal to 10.
|
|
|
|
precision = 10
|
|
|
|
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
# This is the linear search that will occur after the search space has become smaller.
|
2020-10-05 17:44:35 +00:00
|
|
|
def lin_search(left: int, right: int, A: List[int], target: int):
|
2019-10-05 05:14:13 +00:00
|
|
|
for i in range(left, right + 1):
|
|
|
|
if A[i] == target:
|
2017-10-09 02:19:39 +00:00
|
|
|
return i
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
# This is the iterative method of the ternary search algorithm.
|
2020-10-05 17:44:35 +00:00
|
|
|
def ite_ternary_search(A: List[int], target: int):
|
2017-10-09 02:19:39 +00:00
|
|
|
left = 0
|
2019-10-05 05:14:13 +00:00
|
|
|
right = len(A) - 1
|
|
|
|
while True:
|
|
|
|
if left < right:
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if right - left < precision:
|
|
|
|
return lin_search(left, right, A, target)
|
2017-10-09 02:19:39 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
oneThird = (left + right) / 3 + 1
|
|
|
|
twoThird = 2 * (left + right) / 3 + 1
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if A[oneThird] == target:
|
2017-10-09 02:19:39 +00:00
|
|
|
return oneThird
|
2019-10-05 05:14:13 +00:00
|
|
|
elif A[twoThird] == target:
|
2017-10-09 02:19:39 +00:00
|
|
|
return twoThird
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
elif target < A[oneThird]:
|
|
|
|
right = oneThird - 1
|
|
|
|
elif A[twoThird] < target:
|
|
|
|
left = twoThird + 1
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
left = oneThird + 1
|
|
|
|
right = twoThird - 1
|
2017-10-09 02:19:39 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
# This is the recursive method of the ternary search algorithm.
|
2020-10-05 17:44:35 +00:00
|
|
|
def rec_ternary_search(left: int, right: int, A: List[int], target: int):
|
2019-10-05 05:14:13 +00:00
|
|
|
if left < right:
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if right - left < precision:
|
|
|
|
return lin_search(left, right, A, target)
|
2017-10-09 02:19:39 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
oneThird = (left + right) / 3 + 1
|
|
|
|
twoThird = 2 * (left + right) / 3 + 1
|
2017-10-09 02:19:39 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if A[oneThird] == target:
|
2017-10-09 02:19:39 +00:00
|
|
|
return oneThird
|
2019-10-05 05:14:13 +00:00
|
|
|
elif A[twoThird] == target:
|
2017-10-09 02:19:39 +00:00
|
|
|
return twoThird
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
elif target < A[oneThird]:
|
|
|
|
return rec_ternary_search(left, oneThird - 1, A, target)
|
|
|
|
elif A[twoThird] < target:
|
|
|
|
return rec_ternary_search(twoThird + 1, right, A, target)
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
return rec_ternary_search(oneThird + 1, twoThird - 1, A, target)
|
2017-10-09 02:19:39 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
# This function is to check if the array is sorted.
|
2020-10-05 17:44:35 +00:00
|
|
|
def __assert_sorted(collection: List[int]) -> bool:
|
2017-10-09 02:19:39 +00:00
|
|
|
if collection != sorted(collection):
|
2019-10-05 05:14:13 +00:00
|
|
|
raise ValueError("Collection must be sorted")
|
2017-10-09 02:19:39 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
user_input = input("Enter numbers separated by coma:\n").strip()
|
|
|
|
collection = [int(item) for item in user_input.split(",")]
|
2017-10-09 02:19:39 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
__assert_sorted(collection)
|
|
|
|
except ValueError:
|
2019-10-05 05:14:13 +00:00
|
|
|
sys.exit("Sequence must be sorted to apply the ternary search")
|
2017-10-09 02:19:39 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
target_input = input("Enter a single number to be found in the list:\n")
|
2017-10-09 02:19:39 +00:00
|
|
|
target = int(target_input)
|
|
|
|
result1 = ite_ternary_search(collection, target)
|
2019-10-05 05:14:13 +00:00
|
|
|
result2 = rec_ternary_search(0, len(collection) - 1, collection, target)
|
2019-08-19 13:37:49 +00:00
|
|
|
|
2017-10-09 02:19:39 +00:00
|
|
|
if result2 is not None:
|
2019-12-07 05:39:59 +00:00
|
|
|
print(f"Iterative search: {target} found at positions: {result1}")
|
|
|
|
print(f"Recursive search: {target} found at positions: {result2}")
|
2017-10-09 02:19:39 +00:00
|
|
|
else:
|
2019-10-05 05:14:13 +00:00
|
|
|
print("Not found")
|