forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ternary_Search.py
44 lines (31 loc) · 934 Bytes
/
Ternary_Search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Function for Ternary Search
def Ternary_Search(list, left, right, desired):
while left <= right:
# Return positon if found
mid1 = left + int((right - left) / 3);
mid2 = mid1 + int((right - left) / 3);
if list[mid1] == desired:
return mid1
if list[mid2] == desired:
return mid2
if list[mid1] > desired:
return Ternary_Search(list, left, mid1 - 1, desired)
if list[mid2] < desired:
return Ternary_Search(list, mid2 + 1, right, desired)
return Ternary_Search(list, mid1 + 1, mid2 - 1, desired)
return -1
list = [1, 2, 3, 4, 5, 6, 7]
# Element to be searched is 4
if(Ternary_Search(list, 0, 6, 4) != -1):
print("Found")
else:
print("Not Found")
# Element to be searched is 9
if(Ternary_Search(list, 0, 6, 9) != -1):
print("Found")
else:
print("Not Found")
''' Output
Found
Not Found
'''