forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTernary_Search.rb
55 lines (52 loc) · 1.57 KB
/
Ternary_Search.rb
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
45
46
47
48
49
50
51
52
53
54
55
=begin
Ternary Search
-------------------------------
Ternary search is a searching technique that is used to search the position of a specific value in an array.
Ternary search is a divide-and-conquer algorithm.
It is mandatory for the array to be sorted (in which you will search for an element).
The array is divided into three parts and then we determine in which part the element exists.
In this search, after each iteration it neglects ⅓ part of the array and repeats the same operations on the remaining ⅔.
Time Complexity: O(log3 n)
Space Complexity: O(1)
=end
def ternarySearch(l, r, key, ar) #function to perform ternary search
if (r >= l)
#find mid1 and mid2
mid1 = l + (r - l) / 3
mid2 = r - (r - l) / 3
if ar[mid1] == key #check if key is equal to mid1
mid1
elsif ar[mid2] == key #check if key is equal to mid2
mid2
#Since key is not present at mid, check in which region it is present
#then repeat the Search operation in that region
elsif key < ar[mid1]
ternarySearch(l, mid1 - 1, key, ar)
elsif (key > ar[mid2])
ternarySearch(mid2 + 1, r, key, ar)
else
ternarySearch(mid1 + 1, mid2 - 1, key, ar)
end
end
end
n= gets.to_i #length of array
arr = []
while n>0
num = gets.chomp.to_i
arr.push(num)
n = n-1
end
number = gets.to_i #number whose index is to be searched
puts "Index of #{number} is #{ternarySearch(0, arr.length - 1, number, arr)}"
=begin
input->
5
21
34
54
67
78
67
output->
Index of 67 is 3
=end