-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.cpp
More file actions
358 lines (303 loc) · 5.6 KB
/
Sort.cpp
File metadata and controls
358 lines (303 loc) · 5.6 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//Name - Mohit Jaiswal
//Batch - A4
//Roll No. - 57
#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');
}
//OUTPUT
/*Enter number of student
5
Enter marks of students
21
78
90
54
3
***Menu***
1.display
2.linear Search
3.binary Search
4.sentinal search
5.fibonacii search
Enter your choice
1
Marks of student
21
78
90
54
3
Do you want to continue
y
***Menu***
1.display
2.linear Search
3.binary Search
4.sentinal search
5.fibonacii search
Enter your choice
2
Enter mark to be search
78
Your mark found at position :2
Do you want to continue
y
***Menu***
1.display
2.linear Search
3.binary Search
4.sentinal search
5.fibonacii search
Enter your choice
3
Sorted Array
Marks of student
3
21
54
78
90
Enter mark to be search
54
Your mark found at position :3
Do you want to continue
y
***Menu***
1.display
2.linear Search
3.binary Search
4.sentinal search
5.fibonacii search
Enter your choice
4
Enter mark to be searched
1
Your mark not found
Do you want to continue
y
***Menu***
1.display
2.linear Search
3.binary Search
4.sentinal search
5.fibonacii search
Enter your choice
5
Your sorted array
Marks of student
3
21
54
78
90
>>
Enter marks to be search : 90
Given Marks found at position : 5
Given Marks found at position : 5
Do you want to continue
n
--------------------------------
Process exited after 76.59 seconds with return value 0
Press any key to continue . . .*/