1+ def ternary_search (arr , target ):
2+ """
3+ Performs ternary search on a sorted array to find the index of the target element.
4+
5+ Ternary search is a divide and conquer algorithm that can find the position
6+ of a target value within a sorted array. It works by repeatedly dividing
7+ the search interval into three parts.
8+
9+ Args:
10+ arr (list): A sorted list of elements.
11+ target: The element to search for.
12+
13+ Returns:
14+ int: The index of the target element if found, otherwise -1.
15+ """
16+ left , right = 0 , len (arr ) - 1
17+
18+ while left <= right :
19+ # Calculate mid1 and mid2
20+ mid1 = left + (right - left ) // 3
21+ mid2 = right - (right - left ) // 3
22+
23+ # If target is at mid1
24+ if arr [mid1 ] == target :
25+ return mid1
26+ # If target is at mid2
27+ if arr [mid2 ] == target :
28+ return mid2
29+
30+ # If target is in the left third
31+ if target < arr [mid1 ]:
32+ right = mid1 - 1
33+ # If target is in the right third
34+ elif target > arr [mid2 ]:
35+ left = mid2 + 1
36+ # If target is in the middle third
37+ else :
38+ left = mid1 + 1
39+ right = mid2 - 1
40+ return - 1
41+
42+ def ternary_search_recursive (arr , target , left , right ):
43+ """
44+ Performs recursive ternary search on a sorted array to find the index of the target element.
45+
46+ Args:
47+ arr (list): A sorted list of elements.
48+ target: The element to search for.
49+ left (int): The left boundary of the current search space.
50+ right (int): The right boundary of the current search space.
51+
52+ Returns:
53+ int: The index of the target element if found, otherwise -1.
54+ """
55+ if left > right :
56+ return - 1
57+
58+ mid1 = left + (right - left ) // 3
59+ mid2 = right - (right - left ) // 3
60+
61+ if arr [mid1 ] == target :
62+ return mid1
63+ if arr [mid2 ] == target :
64+ return mid2
65+
66+ if target < arr [mid1 ]:
67+ return ternary_search_recursive (arr , target , left , mid1 - 1 )
68+ elif target > arr [mid2 ]:
69+ return ternary_search_recursive (arr , target , mid2 + 1 , right )
70+ else :
71+ return ternary_search_recursive (arr , target , mid1 + 1 , mid2 - 1 )
72+
73+ if __name__ == "__main__" :
74+ # Test cases for iterative ternary search
75+ print ("Iterative Ternary Search Tests:" )
76+ test_array = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
77+ print (f"Array: { test_array } " )
78+
79+ # Test case 1: Target found in the left third
80+ target1 = 2
81+ result1 = ternary_search (test_array , target1 )
82+ print (f"Target { target1 } : Index { result1 } (Expected: 1)" )
83+ assert result1 == 1
84+
85+ # Test case 2: Target found in the middle third
86+ target2 = 5
87+ result2 = ternary_search (test_array , target2 )
88+ print (f"Target { target2 } : Index { result2 } (Expected: 4)" )
89+ assert result2 == 4
90+
91+ # Test case 3: Target found in the right third
92+ target3 = 9
93+ result3 = ternary_search (test_array , target3 )
94+ print (f"Target { target3 } : Index { result3 } (Expected: 8)" )
95+ assert result3 == 8
96+
97+ # Test case 4: Target not found
98+ target4 = 11
99+ result4 = ternary_search (test_array , target4 )
100+ print (f"Target { target4 } : Index { result4 } (Expected: -1)" )
101+ assert result4 == - 1
102+
103+ # Test case 5: Target at the beginning
104+ target5 = 1
105+ result5 = ternary_search (test_array , target5 )
106+ print (f"Target { target5 } : Index { result5 } (Expected: 0)" )
107+ assert result5 == 0
108+
109+ # Test case 6: Target at the end
110+ target6 = 10
111+ result6 = ternary_search (test_array , target6 )
112+ print (f"Target { target6 } : Index { result6 } (Expected: 9)" )
113+ assert result6 == 9
114+
115+ # Test case 7: Empty array
116+ empty_array = []
117+ target7 = 5
118+ result7 = ternary_search (empty_array , target7 )
119+ print (f"Target { target7 } in empty array: Index { result7 } (Expected: -1)" )
120+ assert result7 == - 1
121+
122+ # Test case 8: Single element array - target found
123+ single_element_array = [7 ]
124+ target8 = 7
125+ result8 = ternary_search (single_element_array , target8 )
126+ print (f"Target { target8 } in single element array: Index { result8 } (Expected: 0)" )
127+ assert result8 == 0
128+
129+ # Test case 9: Single element array - target not found
130+ target9 = 8
131+ result9 = ternary_search (single_element_array , target9 )
132+ print (f"Target { target9 } in single element array: Index { result9 } (Expected: -1)" )
133+ assert result9 == - 1
134+
135+ print ("\n Recursive Ternary Search Tests:" )
136+ # Test cases for recursive ternary search
137+ test_array_rec = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ]
138+ print (f"Array: { test_array_rec } " )
139+
140+ # Test case 1: Target found in the left third
141+ target_rec1 = 2
142+ result_rec1 = ternary_search_recursive (test_array_rec , target_rec1 , 0 , len (test_array_rec ) - 1 )
143+ print (f"Target { target_rec1 } : Index { result_rec1 } (Expected: 1)" )
144+ assert result_rec1 == 1
145+
146+ # Test case 2: Target found in the middle third
147+ target_rec2 = 5
148+ result_rec2 = ternary_search_recursive (test_array_rec , target_rec2 , 0 , len (test_array_rec ) - 1 )
149+ print (f"Target { target_rec2 } : Index { result_rec2 } (Expected: 4)" )
150+ assert result_rec2 == 4
151+
152+ # Test case 3: Target found in the right third
153+ target_rec3 = 9
154+ result_rec3 = ternary_search_recursive (test_array_rec , target_rec3 , 0 , len (test_array_rec ) - 1 )
155+ print (f"Target { target_rec3 } : Index { result_rec3 } (Expected: 8)" )
156+ assert result_rec3 == 8
157+
158+ # Test case 4: Target not found
159+ target_rec4 = 11
160+ result_rec4 = ternary_search_recursive (test_array_rec , target_rec4 , 0 , len (test_array_rec ) - 1 )
161+ print (f"Target { target_rec4 } : Index { result_rec4 } (Expected: -1)" )
162+ assert result_rec4 == - 1
163+
164+ # Test case 5: Target at the beginning
165+ target_rec5 = 1
166+ result_rec5 = ternary_search_recursive (test_array_rec , target_rec5 , 0 , len (test_array_rec ) - 1 )
167+ print (f"Target { target_rec5 } : Index { result_rec5 } (Expected: 0)" )
168+ assert result_rec5 == 0
169+
170+ # Test case 6: Target at the end
171+ target_rec6 = 10
172+ result_rec6 = ternary_search_recursive (test_array_rec , target_rec6 , 0 , len (test_array_rec ) - 1 )
173+ print (f"Target { target_rec6 } : Index { result_rec6 } (Expected: 9)" )
174+ assert result_rec6 == 9
175+
176+ # Test case 7: Empty array
177+ empty_array_rec = []
178+ target_rec7 = 5
179+ result_rec7 = ternary_search_recursive (empty_array_rec , target_rec7 , 0 , len (empty_array_rec ) - 1 )
180+ print (f"Target { target_rec7 } in empty array: Index { result_rec7 } (Expected: -1)" )
181+ assert result_rec7 == - 1
182+
183+ # Test case 8: Single element array - target found
184+ single_element_array_rec = [7 ]
185+ target_rec8 = 7
186+ result_rec8 = ternary_search_recursive (single_element_array_rec , target_rec8 , 0 , len (single_element_array_rec ) - 1 )
187+ print (f"Target { target_rec8 } in single element array: Index { result_rec8 } (Expected: 0)" )
188+ assert result_rec8 == 0
189+
190+ # Test case 9: Single element array - target not found
191+ target_rec9 = 8
192+ result_rec9 = ternary_search_recursive (single_element_array_rec , target_rec9 , 0 , len (single_element_array_rec ) - 1 )
193+ print (f"Target { target_rec9 } in single element array: Index { result_rec9 } (Expected: -1)" )
194+ assert result_rec9 == - 1
0 commit comments