-
Notifications
You must be signed in to change notification settings - Fork 0
/
11.STUDENT_SEARCH.cpp
185 lines (170 loc) · 3.28 KB
/
11.STUDENT_SEARCH.cpp
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
//oral questions on best and worst case complexity
/*n=no. of comparison
sequential/linear search = O(n) ,sentinel search=O(n) ,binary search=O(log2n)
,fibonacci search=O(log n)*/
#include<iostream>
#define N 6
using namespace std;
class search
{
private:
int s[N];
int i;
int n;
public:
//----------------------------------------------------------------
search()
{
for(i=0;i<N;i++)
{
cout<<"Enter no. : ";
cin>>n;
s[i]=n;
}
cout<<"\n***ALL NUMBERS ENTER***";
}
//----------------------------------------------------------------
void linear_search()
{
int search_no=0;
cout<<"\nEnter no. to search : ";
cin>>search_no;
for(i=0;i<N;i++)
{
if(search_no == s[i])
{
cout<<"NUMBER FOUND";
return;
}
}
cout<<"Number not found";
}
//----------------------------------------------------------------
void sentinel_search()
{
int search_no;
int last;
last = s[N-1];
cout<<"\nEnter number to be found : ";
cin>>search_no;
s[N-1]=search_no;
for(i=0;s[i]!=search_no;i++)
{
}
s[N-1]=last;
if(i<N-1 || last==search_no)
cout<<"\nNumber found";
else
cout<<"Number not found";
}
//----------------------------------------------------------------
void binary_search()//fastest search
{
int i,j,k,temp;
int search_no=0;
for(i=0;i<N;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(s[j]<s[k])
{
k=j;
}
}
temp=s[i];
s[i]=s[k];
s[k]=temp;
}
//selection sort completed
cout<<"\nEnter no. to search : ";
cin>>search_no;
int start=0,end=N-1,mid;
while(start<=end)
{
mid=(start+end)/2;
if(s[mid] == search_no)
{
cout<<"NUMBER FOUND";
return;
}
if(s[mid] > search_no)
{
end = mid-1;
}
if(s[mid] < search_no)
{
start=mid+1;
}
}
cout<<"\nNUMBER NOT FOUND";
}
//----------------------------------------------------------------
void display_nos()
{
for(i=0;i<N;i++)
{
cout<<"Number : "<<s[i]<<endl;
}
cout<<"\n***ALL DISPLAYED***\n";
}
//----------------------------------------------------------------
void fibonacci_search()//not done yet
{
int i,j,k,temp;
int search_no;
for(i=0;i<N;i++)
{
k=i;
for(j=i+1;j<=5;j++)
{
if(s[j]<s[k])
{
k=j;
}
}
temp=s[i];
s[i]=s[k];
s[k]=temp;
}
//selection sort completed
}
//----------------------------------------------------------------
};
main()
{
search s1;
int op=-1;
while(op!=0)
{
cout<<"\n\n1.Linear search";
cout<<"\n2.Sentinel search";
cout<<"\n3.Binary search";
cout<<"\n4.Fibonacci search";
cout<<"\n5.Display";
cout<<"\n6.Exit";
cout<<"\n\nEnter Choice - ";
cin>>op;
switch(op)
{
case 1:
s1.linear_search();
break;
case 2:
s1.sentinel_search();
break;
case 3:
s1.binary_search();
break;
case 4:
s1.fibonacci_search();
break;
case 5:
s1.display_nos();
break;
case 6:
op=0;
break;
}
}
}