-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearching.cpp
More file actions
255 lines (210 loc) · 4.27 KB
/
Searching.cpp
File metadata and controls
255 lines (210 loc) · 4.27 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Name - Mohit Jaiswal
Roll no. - 57
Practical No. - 10
*/
#include <iostream>
#define max 30
using namespace std;
class student
{
public:
int mark[max],n; //array of marks
void getdata();
void display();
void sort();
void linear();
void binary();
void sentinal();
void fibo();
};
void student::getdata() //marks of student
{
cout<<"Enter number of student"<<endl;
cin>>n;
cout<<"Enter marks of students"<<endl;
for(int i=0;i<n;i++)
{
cin>>mark[i];
}
}
void student::display()
{
cout<<"Marks of student"<<endl;
for(int i=0;i<n;i++)
{
cout<<mark[i]<<endl;
}
}
void student::sort() //sorting of marks
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(mark[j]<mark[i])
{
int temp=mark[j];
mark[j]=mark[i];
mark[i]=temp;
}
}
}
}
void student::linear() //linear search
{
int key,flag=0;
cout<<"Enter mark to be search"<<endl;
cin>>key;
for(int i=0;i<n;i++)
{
if(mark[i]==key)
{
cout<<"Your mark found at position :"<<i+1<<endl;
flag=1;
break;
}
}
if(flag!=1)
{
cout<<"Your mark not found"<<endl;
}
}
void student::binary() //binary search
{
int key,flag=0;
int low=0,high=n-1;
int mid;
cout<<"Enter mark to be search"<<endl;
cin>>key;
while(low<=high)
{ mid=low+(high-low)/2;
if(mark[mid]==key)
{
cout<<"Your mark found at position :"<<mid+1<<endl;
flag=1;
break;
}
else
{
if(key<mark[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
}
if(flag!=1)
{
cout<<"Your mark not found"<<endl;
}
}
void student::sentinal() //sentinal search
{
int last,key,i=0;
cout<<"Enter mark to be searched"<<endl;
cin>>key;
last=mark[n-1];
mark[n-1]=key;
while(mark[i]!=key)
i++;
mark[n-1]=last;
if(i<(n-1) || mark[i]==key)
{
cout<<"Your mark is found at position :"<<i+1<<endl;
}
else
{
cout<<"Your mark not found"<<endl;
}
}
void student::fibo() //fibonacy search
{
int key;
cout<<">>\nEnter marks to be search : ";
cin>>key;
int offset = 0;
int fb_prev2 = 0;
int fb_prev1 = 1;
int fb_curr = fb_prev2 + fb_prev1;
while (fb_curr < n)
{
fb_prev2 = fb_prev1;
fb_prev1 = fb_curr;
fb_curr = fb_prev2 + fb_prev1;
}
int i=0;
while (fb_curr > 1)
{
i = min(offset+fb_prev2, n-1);
if (mark[i] < key)
{
fb_curr = fb_prev1;
fb_prev1 = fb_prev2;
fb_prev2 = fb_curr - fb_prev1;
offset = i;
}
else if (mark[i] > key)
{
fb_curr = fb_prev2;
fb_prev1 = fb_prev1 - fb_prev2;
fb_prev2 = fb_curr - fb_prev1;
}
else
{
cout<<" \nGiven Marks found at position : "<<i+1<<endl;
break;
}
}
if(fb_prev1 && mark[offset+1] == key)
{
cout<<" \nGiven Marks found at position : "<<offset+2<<endl;
}
else
{
cout<<"\nMarks was not found"<<endl;
}
}
int main()
{
student s;
int choice;
char ch;
s.getdata();
do
{
cout<<"***Menu***"<<endl;
cout<<"\n1.display\n2.linear Search\n3.binary Search\n4.sentinal search"<<endl;
cout<<"5.fibonacii search"<<endl;
cout<<"Enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1: //display
s.display();
break;
case 2: //linear search
s.linear();
break;
case 3: //binary search
cout<<"Sorted Array"<<endl; //first sorting
s.sort();
s.display();
s.binary();
break;
case 4: //sentinal search
s.sentinal();
break;
case 5: //fibonacy search
s.sort(); //first sorting
cout<<"Your sorted array"<<endl;
s.display();
s.fibo();
}
cout<<"Do you want to continue"<<endl;
cin>>ch;
}while(ch=='y');
}