forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.cpp
210 lines (193 loc) · 4.36 KB
/
1.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
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
//
// dsatrial1.cpp
//
//
// Created by Vansh Vatsal on 30/08/22.
//
#include <bits/stdc++.h>
using namespace std;
class SEIT
{
string name;
int roll;
float sgpa;
public:
void getdata()
{
string a;
int b;
float c;
cout << "Enter Name : " << endl;
cin >> a;
cout << "Enter Roll No. : " << endl;
cin >> b;
cout << "Enter SGPA : " << endl;
cin >> c;
name = a;
roll = b;
sgpa = c;
}
void putdata()
{
cout << name << endl;
cout << roll << endl;
cout << sgpa << endl;
}
void linearsearch(SEIT s[], int n, float key)
{
bool flag = 1;
for (int i = 0; i < n; i++)
{
if (s[i].sgpa == key)
{
cout << "Object is present at " << i << " index." << endl;
s[i].putdata();
flag = 0;
}
}
if (flag)
{
cout << "No record found" << endl;
}
}
void binarysearch(SEIT s[], int n, string nm)
{
int start = 0;
int end = n - 1;
int mid = (start + end) / 2;
bool flag = 1;
while (start <= end)
{
if (s[mid].name == nm)
{
s[mid].putdata();
flag = 0;
break;
}
else if (s[mid].name < nm)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
mid = (start + end) / 2;
}
if (flag)
{
cout << "No record found." << endl;
}
}
void bubblesort(SEIT s[], int n)
{
for (int l = 0; l < n; ++l)
{
for (int i = 0; i < n -l-1; ++i)
{
if ( s[i].roll > s[i+1].roll)
{
swap(s[i],s[i+1]);
}
}
}
}
void insertionSort( SEIT s[], int size) {
for (int step = 1; step < size; step++) {
int key = s[step];
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (key < s[j] && j >= 0) {
s[j + 1] = s[j];
--j;
}
s[j + 1] = key;
}
}
void printArray(SEIT s[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << "roll " << s[i].roll<<endl;
cout << "name " << s[i].name<<endl;
cout << "sgpa " << s[i].sgpa<<endl;
}
cout << "\n";
}
};
int
menu()
{
int a;
cout << " 1 for linear search" << endl;
cout << " 2 for binary search" << endl;
cout << " 3 to display details of all students" << endl;
cout << " 4 to exit" << endl;
cout << " 5 to do Bubble Sort" << endl;
cout << " 6 to do Insertion Sort" << endl;
cout << "enter your choice = ";
cin >> a;
cout << endl;
return a;
}
int main()
{
cout << "enter number of students" << endl;
int n;
cin >> n;
SEIT s[n];
for (int i = 0; i < n; i++)
{
s[i].getdata();
}
int status;
status = menu();
while (true)
{
SEIT task;
if (status == 4)
{
break;
}
else if (status == 1)
{
float key;
cout << "Enter SGPA of the student : " << endl;
cin >> key;
task.linearsearch(s, n, key);
}
else if (status == 2)
{
string nm;
cout << "Enter the name of the student : " << endl;
cin >> nm;
task.binarysearch(s, n, nm);
}
else if (status == 3)
{
for (int i = 0; i < n; i++)
{
s[i].getdata();
}
}
else if (status == 5)
{
task.bubblesort(s, n);
cout << "Sorted Array in Ascending Order:\n";
task.printArray(s,n);
}
else if (status==6){
task.insertionSort(s,size);
cout<<"Insertion Sort"<<endl;
task.printArray(s,size);
}
else
{
cout << "invalid input pls enter another choice" << endl;
}
status = menu();
}
return 0;
}