forked from KIET7UKE/HacktoberRepo-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathternary serach.c
74 lines (74 loc) · 1.37 KB
/
ternary serach.c
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
67
68
69
70
71
72
73
74
#include<stdio.h>
#include<stdlib.h>
int main()
{
int ts(int*,int,int,int);
void bsort(int*,int);
int *a,n,se,i,r;
printf("enter the array size=");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
if(a==NULL)
{
exit(0);
}
printf("enter array elements=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bsort(a,n);
printf("the sorted array=");
for(i=0;i<n;i++)
{
printf(" %d ",a[i]);
}
printf("\nenter search elements=");
scanf("%d",&se);
r=ts(a,0,n-1,se);
if(r==-1)
{
printf("element not found");
}
else
{
printf("element found is at %d index",r);
}
}
void bsort(int*b,int s)
{
int i,temp;
do
{
for(i=1;i<s;i++)
{
if(b[i]<b[i-1])
{
temp=b[i];
b[i]=b[i-1];
b[i-1]=temp;
}
}
s--;
}while(s>0);
}
int ts(int*b,int l,int h, int se)
{
int m1,m2;
if(l<=h)
{
m1=(l+(h-l)/3);
m2=(m1+(h-l)/3);
if(b[m1]==se)
return m1;
else if(b[m2]==se)
return m2;
else if(se<b[m1])
return ts(b,l,m1-1,se);
else if(se>b[m1] && se<b[m2])
return ts(b,m1+1,m2-1,se);
else
return ts(b,m2+1,h,se);
}
return -1;
}