-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathTernary_search.cpp
More file actions
66 lines (51 loc) · 1.15 KB
/
Ternary_search.cpp
File metadata and controls
66 lines (51 loc) · 1.15 KB
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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;
// Function for Ternary Search
int ternarySearch(int l, int r, int key, int ar[])
{
while (r >= l)
{
int mid1 = l + (r - l) / 3; //finding mid
int mid2 = r - (r - l) / 3;
// Check if key is present at any mid
if (ar[mid1] == key) {
return mid1;
}
if (ar[mid2] == key) {
return mid2;
}
if (key < ar[mid1]) {
// The key lies in between l and mid1
r = mid1 - 1;
}
else if (key > ar[mid2]) {
// The key is in between mid2 and r
l = mid2 + 1;
}
else {
// The key is in between mid1 and mid2
l = mid1 + 1;
r = mid2 - 1;
}
}
return -1;
}
// Driver code
int main()
{
int l, r, ans ,key,n;
cin>>n>>key;
int ar[n];
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
// Starting index
l = 0;
// length of array
r = n-1;
// Search the key using ternarySearch
ans = ternarySearch(l, r, key, ar);
// Print the result
cout << ans<< endl;
}