forked from Akanksha1212/C_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ternary_search.cpp
42 lines (37 loc) · 918 Bytes
/
ternary_search.cpp
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
#include<bits/stdc++.h>
using namespace std;
int ternarySearch(int low, int high, int *a, int key)
{
int mid1 = low + (high - low) / 3;
int mid2 = high - (high - low) / 3;
if(high>=low)
{
if(key==a[mid1])
return mid1;
if(key==a[mid2])
return mid2;
if(key<a[mid1])
return ternarySearch(low, mid1-1, a, key);
else if(key>a[mid2])
return ternarySearch(mid2+1, high, a, key);
else
return ternarySearch(mid1+1, mid2-1, a, key);
}
return -1;
}
int main()
{
int i,n;
cout<<"enter array size:";
cin>>n;
int a[n];
cout<<"enter array:";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int k;
cout<<"enter key";
cin>>k;
cout<<"found at position: "<<ternarySearch(0,n-1,a,k)<<"\n";
return 0;
}