-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfds5.txt
170 lines (170 loc) · 2.58 KB
/
fds5.txt
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
using namespace std;
class Search{
int n;
int roll[];
public:
Search()
{
cout<<"Enter the number of students who attended the training program :
";
cin>>n;
//roll = new int[n+1];
}
void getRoll()
{
cout<<"Enter the roll numbers of the students who attended the training
program -"<<endl;
for(int i=0; i<n; i++)
{
cout<<"Enter roll number of student "<<i+1<<" : ";
cin>>roll[i];
}
}
void linearSearch(int key)
{
int flag = 0;
for(int i=0; i<n; i++)
{
if(roll[i] == key)
{
flag = 1;
break;
}
}
if(flag == 1)
cout<<"Roll number "<<key<<" was present for the training
program."<<endl;
else
cout<<"Roll number "<<key<<" did not attend the training
program."<<endl;
}
void sentinelSearch(int key)
{
roll[n] = key;
int i = 0;
while(roll[i]!=key)
{
i++;
}
if(i<n)
cout<<"Roll number "<<key<<" was present for the training
program."<<endl;
else
cout<<"Roll number "<<key<<" did not attend the training
program."<<endl;
}
void binarySearch(int key)
{
int low = 0;
int high = n-1;
int mid, flag = 0;
while(low <= high)
{
mid = (low+high)/2;
if(roll[mid] == key)
{
flag = 1;
break;
}
else if(roll[mid] < key)
low = mid+1;
else
high = mid-1;
}
if(flag == 1)
cout<<"Roll number "<<key<<" was present for the training
program."<<endl;
else
cout<<"Roll number "<<key<<" did not attend the training
program."<<endl;
}
int fibo(int j)
{
if(j==0)
{
return 0;
}
if(j==1)
{
return 1;
}
else
{
return((fibo(j-1))+(fibo(j-2)));
}
}
bool fibosearch(int key)
{
int f1,f2,j,mid;
j=1;
while(fibo(j)<=n)
{
j++;
}
f1=fibo(j-2);
f2=fibo(j-3);
mid=n-f1+1;
while(key!=roll[mid])
{
if(key>roll[mid])
{
if(f1==1)
{
break;
mid=mid+f2;
f1=f1-f2;
f2=f2-f1;
}
else
{
if(f2==0)
{
break;
mid=mid-f2;
int temp=f1-f2;
f1=f2;
f2=temp;
}
}
}
}
if(roll[mid]==key)
{
cout<<"roll number "<<key<<" was present "<<endl;
}
else
{
cout<<"roll number "<<key<<" was not present "<<endl;
}
}
};
int main()
{
Search ob;
ob.getRoll();
int key, ch;
cout<<"Enter the roll number which you want to search for : ";
cin>>key;
cout<<"\nMenu -\n1. Linear Search\n2. Sentinel Search\n3. Binary
Search\n4. Fibo Search"<<endl;
cout<<"Enter your choice - 1, 2, 3 or 4 : ";
cin>>ch;
switch(ch){
case 1:
ob.linearSearch(key);
break;
case 2:
ob.sentinelSearch(key);
break;
case 3:
ob.binarySearch(key);
break;
case 4:
ob.fibosearch(key);
break;
default:
cout<<"Please enter valid choice next time.";
}
return 0;
}