forked from prashantkalokhe/Hactoberfest-2022-New
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesortnew.cpp
204 lines (173 loc) · 3.54 KB
/
bubblesortnew.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
#include <iostream>
using namespace std;
class sort
{
float a[100],b[100];
int n,i,j;
public:
void enter();
void display();
void selection();
void bubblesort();
void dispbubble();
};
void sort::enter()
{
cout<<"Enter the number of students whose percentage is to be stored: ";
cin>>n;
cout<<endl;
cout<<"Enter the percentage of students:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
cout<<endl;
}
}
void sort::display()
{
cout<<"\nThe list of scores you entered are as follows:\n";
for(i=0;i<n;i++)
{
cout<<b[i]<<"\n\n";
}
}
void sort::selection()
{
int pos;
float min,temp;
for(i=0;i<n-1;i++)
{
min=a[i];
pos=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
pos=j;
}
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
cout<<"\nThe list of scores in ascending order using Selection Sort is as follows:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n\n";
a[i]=b[i];
}
}
void sort::bubblesort()
{
float temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void sort::dispbubble()
{
cout<<"\nThe list of scores in ascending order using Bubble sort is as follows:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n\n";
a[i]=b[i];
}
}
int main()
{
sort obj;
int ch,i;
do
{
cout<<endl;
cout<<"Choose the operation to be performed:"
"\n1.Enter the percentage of students."
"\n2.Display the scores in the format the user entered them."
"\n3.Display the scores in ascending order using Bubble sort."
"\n4.Display the scores in ascending order using Selection Sort."
"\n5.Exit.\n";
cout<<endl;
cin>>ch;
switch(ch)
{
case 1:obj.enter();
break;
case 2:obj.display();
break;
case 3:obj.bubblesort();
obj.dispbubble();
break;
case 4:obj.selection();
break;
case 5:break;
default: cout<<"Enter a valid choice.";
break;
}
}while(ch!=5);
return 0;
}
/* OUTPUT
Choose the operation to be performed:
1.Enter the percentage of students.
2.Display the scores in the format the user entered them.
3.Display the scores in ascending order using Bubble sort.
4.Display the scores in ascending order using Selection Sort.
5.Exit.
1
Enter the number of students whose percentage is to be stored: 3
Enter the percentage of students:
80
90
75
Choose the operation to be performed:
1.Enter the percentage of students.
2.Display the scores in the format the user entered them.
3.Display the scores in ascending order using Bubble sort.
4.Display the scores in ascending order using Selection Sort.
5.Exit.
2
The list of scores you entered are as follows:
80
90
75
Choose the operation to be performed:
1.Enter the percentage of students.
2.Display the scores in the format the user entered them.
3.Display the scores in ascending order using Bubble sort.
4.Display the scores in ascending order using Selection Sort.
5.Exit.
3
The list of scores in ascending order using Bubblesort is as follows:
75
80
90
Choose the operation to be performed:
1.Enter the percentage of students.
2.Display the scores in the format the user entered them.
3.Display the scores in ascending order using Bubble sort.
4.Display the scores in ascending order using Selection Sort.
5.Exit.
4
The list of scores in ascending order using Selection Sort is as follows:
75
80
90
Choose the operation to be performed:
1.Enter the percentage of students.
2.Display the scores in the format the user entered them.
3.Display the scores in ascending order using Bubble sort.
4.Display the scores in ascending order using Selection Sort.
5.Exit.
5
*/