-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ020.cpp
More file actions
67 lines (59 loc) · 1.62 KB
/
Copy pathQ020.cpp
File metadata and controls
67 lines (59 loc) · 1.62 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
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
#define F_OR(i, a, b) for (int i=(a); i<(b); i++)
using namespace std;
void sort_ghammosi (string a[], int n){
for (int i=1;i<n;i++){
// based on bubble sort
int bubbleMove = n-i;
int changeCnt = 0;
for (int j=0;j<bubbleMove;j++){
if (a[j][0] > a[j+1][0]) {
string tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
changeCnt++;
}
else if (a[j][0] == a[j+1][0]){
if (a[j][1] > a[j+1][1]){
string tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
changeCnt++;
}
}
}
if (changeCnt==0) break;
}
}
void sort_aplhabet (string a[], int n){
// based on bubble sort again
for (int i=1;i<n;i++){
int bubbleMove = n-i;
int changeCnt = 0;
for (int j=0;j<bubbleMove;j++){
if (toupper(a[j][0]) > toupper(a[j+1][0])) {
string tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
changeCnt++;
}
}
if (changeCnt==0) break;
}
}
int main(){
cin.tie(0);
string s[1000];
s[0] = "";
int n=0;
while (s[n-1] != "0") {
cin >> s[n];
n++;
}
n--;
sort_ghammosi(s, n);
sort_aplhabet(s, n);
for(int i=0; i<n; i++) cout << s[i] << " ";
}