-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 11.cpp
More file actions
147 lines (146 loc) · 3.07 KB
/
Assignment 11.cpp
File metadata and controls
147 lines (146 loc) · 3.07 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
//============================================================================
// Name : Assignment 11.cpp
// Author : 21258
// Version :
// Copyright : Your copyright notice
// Description : Searching algorithms (Linear,Sentinel,Binary,Fibonacci)
//============================================================================
#include <iostream>
using namespace std;
class Student
{
public :
int r[100],p;
};
void input(int *a,int n)
{
cout<<"Please enter the roll numbers in ascending order"<<endl;
for(int i=0;i<n;i++)
{
cout<<"Roll number "<<(i+1)<<" : ";
cin>>a[i];
}
}
void linear(int *a,int n)
{
int m,count=0;
cout<<"Enter the roll number that is to be searched for : ";
cin>>m;
for(int i=0;i<n;i++)
{
if(a[i]==m)
{
count++;
cout<<"The student was present"<<endl;
break;
}
}
if(count==0)
cout<<"The student was absent"<<endl;
}
void sentinel(int *a,int n)
{
int m,i;
cout<<"Enter the roll number that is to be searched for : ";
cin>>m;
a[n]=m;
while(a[i]!=m)
i++;
if(i==n)
cout<<"The student was absent"<<endl;
else
cout<<"The student was present"<<endl;
}
void binary(int *a,int ele,int low,int high)
{
int mid;
if(low<=high)
{
mid=(low+high)/2;
if(ele<a[mid])
binary(a,ele,low,mid-1);
else if(ele>a[mid])
binary(a,ele,mid+1,high);
else cout<<"The number was present"<<endl;
}
if(low>high)
cout<<"The number was absent"<<endl;
}
int fibMonaccianSearch(int arr[], int x, int n)
{
int fibMMm2 = 0;
int fibMMm1 = 1;
int fibM = fibMMm2 + fibMMm1;
while (fibM < n)
{
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}
int offset = -1;
while (fibM > 1)
{
int i = min(offset+fibMMm2, n-1);
if (arr[i] < x)
{
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM - fibMMm1;
offset = i;
}
else if (arr[i] > x)
{
fibM = fibMMm2;
fibMMm1 = fibMMm1 - fibMMm2;
fibMMm2 = fibM - fibMMm1;
}
else return i;
}
if(fibMMm1 && arr[offset+1]==x)
return offset+1;
return -1;
}
int main() {
int o,ele,m=0;
char e;
Student ob1;
cout<<"Enter the number of students : ";
cin>>ob1.p;
input(ob1.r,ob1.p);
do
{
cout<<"Enter your choice : \n1.Linear Search\n2.Sentinel Search\n3.Binary Search\n4.Fibonacci Search"<<endl;
cin>>o;
switch(o)
{
case 1:
linear(ob1.r,ob1.p);
break;
case 2:
sentinel(ob1.r,ob1.p);
break;
case 3:
cout<<"Enter the roll number that is to be searched for : ";
cin>>ele;
binary(ob1.r,ele,0,ob1.p-1);
break;
case 4:
cout<<"Enter the element that is to be searched for : ";
cin>>ele;
m=fibMonaccianSearch(ob1.r,ele,ob1.p);
if(m==-1)
cout<<"Number is absent!\n";
else
cout<<"Number found at index "<<m<<"!\n";
break;
default:
cout<<"Please enter valid choice"<<endl;
}
cout<<"Would you like to search for another roll number?"<<endl;
cin>>e;
}
while(e=='Y');
if(e=='N')
cout<<"Thank you for using the program!";
return 0;
}