@@ -82,6 +82,11 @@ def ite_ternary_search(array: list[int], target: int) -> int:
8282 -1
8383 >>> ite_ternary_search([.1, .4 , -.1], .1)
8484 0
85+ >>> test_list_large = list(range(100))
86+ >>> ite_ternary_search(test_list_large, 65)
87+ 65
88+ >>> ite_ternary_search(test_list_large, 105)
89+ -1
8590 """
8691
8792 left = 0
@@ -90,22 +95,22 @@ def ite_ternary_search(array: list[int], target: int) -> int:
9095 if right - left < precision :
9196 return lin_search (left , right , array , target )
9297
93- one_third = ( left + right ) // 3 + 1
94- two_third = 2 * ( left + right ) // 3 + 1
98+ one_third = left + ( right - left ) // 3
99+ two_third = right - ( right - left ) // 3
95100
96101 if array [one_third ] == target :
97102 return one_third
98103 elif array [two_third ] == target :
99104 return two_third
100105
101106 elif target < array [one_third ]:
102- right = one_third - 1
107+ right = one_third
103108 elif array [two_third ] < target :
104109 left = two_third + 1
105110
106111 else :
107112 left = one_third + 1
108- right = two_third - 1
113+ right = two_third
109114 return - 1
110115
111116
@@ -133,24 +138,31 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
133138 -1
134139 >>> rec_ternary_search(0, 3, [.1, .4 , -.1], .1)
135140 0
141+ >>> test_list_large = list(range(100))
142+ >>> rec_ternary_search(0, len(test_list_large), test_list_large, 65)
143+ 65
144+ >>> rec_ternary_search(20, 80, test_list_large, 65)
145+ 65
146+ >>> rec_ternary_search(20, 80, test_list_large, 15)
147+ -1
136148 """
137149 if left < right :
138150 if right - left < precision :
139151 return lin_search (left , right , array , target )
140- one_third = ( left + right ) // 3 + 1
141- two_third = 2 * ( left + right ) // 3 + 1
152+ one_third = left + ( right - left ) // 3
153+ two_third = right - ( right - left ) // 3
142154
143155 if array [one_third ] == target :
144156 return one_third
145157 elif array [two_third ] == target :
146158 return two_third
147159
148160 elif target < array [one_third ]:
149- return rec_ternary_search (left , one_third - 1 , array , target )
161+ return rec_ternary_search (left , one_third , array , target )
150162 elif array [two_third ] < target :
151163 return rec_ternary_search (two_third + 1 , right , array , target )
152164 else :
153- return rec_ternary_search (one_third + 1 , two_third - 1 , array , target )
165+ return rec_ternary_search (one_third + 1 , two_third , array , target )
154166 else :
155167 return - 1
156168
@@ -165,9 +177,10 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
165177 assert collection == sorted (collection ), f"List must be ordered.\n { collection } ."
166178 target = int (input ("Enter the number to be found in the list:\n " ).strip ())
167179 result1 = ite_ternary_search (collection , target )
168- result2 = rec_ternary_search (0 , len (collection ) - 1 , collection , target )
180+ result2 = rec_ternary_search (0 , len (collection ), collection , target )
169181 if result2 != - 1 :
170182 print (f"Iterative search: { target } found at positions: { result1 } " )
171183 print (f"Recursive search: { target } found at positions: { result2 } " )
172184 else :
173185 print ("Not found" )
186+
0 commit comments