-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble sort of name.cpp
More file actions
43 lines (34 loc) · 896 Bytes
/
Bubble sort of name.cpp
File metadata and controls
43 lines (34 loc) · 896 Bytes
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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX_NAMES = 100;
struct Person {
string name;
};
int main() {
Person people[MAX_NAMES];
int n;
// Read in the number of people
cout << "Enter the number of people: ";
cin >> n;
// Read in the names of each person
for (int i = 0; i < n; i++) {
cout << "Enter the name of person " << i + 1 << ": ";
cin >> people[i].name;
}
// Bubble Sort algorithm to sort the names in alphabetical order
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (people[j].name > people[j + 1].name) {
swap(people[j], people[j + 1]);
}
}
}
// Print out the sorted names
cout << "Sorted names: " << endl;
for (int i = 0; i < n; i++) {
cout << people[i].name << endl;
}
return 0;
}