We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 4e5ab32 + ae42d7d commit c98ab50Copy full SHA for c98ab50
Searching/TernarySearch.py
@@ -0,0 +1,51 @@
1
+'''input
2
+10
3
+1
4
+2
5
+3
6
+4
7
+5
8
+6
9
+7
10
+8
11
12
+11
13
14
+'''
15
+size = int(input())
16
+
17
+arr = []
18
19
+for _ in range(size):
20
+ element = int(input())
21
+ arr.append(element)
22
23
+value = int(input()) # value to be searched
24
25
+lo = 0
26
+hi = size - 1
27
+print(arr)
28
+flag = False
29
+while lo <= hi:
30
+ mid1 = lo + (hi-lo)//3
31
+ mid2 = lo + 2*(hi-lo)//3
32
33
+ if arr[mid1] == value:
34
+ print("Value found at index {}".format(mid1))
35
+ flag = True
36
+ break
37
+ if arr[mid2] == value:
38
+ print("Value found at index {}".format(mid2))
39
40
41
42
+ if value < arr[mid1]:
43
+ hi = mid1 - 1
44
+ elif value > arr[mid1]:
45
+ lo = mid2 + 1
46
+ else:
47
+ lo = mid1 + 1
48
+ hi = mid2 - 1
49
50
+if flag == False:
51
+ print("Value Not Found")
0 commit comments