排序

一,第一种排序

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
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5 + 10;

vector<int> a;

int b[N];

int main() {

ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

int n;

cin >> n;

for (int i = 0; i < n; i++) {

int x;

cin >> x;

a.push_back(x);

}

sort(a.begin(), a.end());//库里面自带的快排函数

a.erase(unique(a.begin(), a.end()), a.end());//去重操作
//unique()将重复的数字放在后面,然后返回一个迭代器,在重复的数字那里
//erase消除两个迭代器的位置
for (auto& x : a) {//遍历
cout << x << ' ';
}

}

二,第二种排序

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
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5;
//结构体排序
struct Book {

int high, thick, width;

} b[N];
//cmp即使改变sort排序顺序
bool cmp(const Book &a, const Book &b) {

if (a.high == b.high && a.thick == b.thick) return a.width > b.width;

if (a.high == b.high) return a.thick > b.thick;

return a.high > b.high;

}

int main() {

ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

int n;

cin >> n;

for (int i = 1; i <= n; i++) {

cin >> b[i].high >> b[i].thick >> b[i].width;

}

sort(b + 1, b + n + 1, cmp);

for (int i = 1; i <= n; i++) {

cout << b[i].high << ' ' << b[i].thick << ' ' << b[i].width << '\n';

}

}

三,第三种排序

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
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 2e5 + 10;

int a[N];
//桶排序
int main() {

ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

int n;

cin >> n;

for (int i = 1; i <= n; i++) {

int x;

cin >> x;

a[x]++;//将值对应的数组中++

}

for (int i = 0; i <= 2e5; i++) {

for (int j = 0; j < a[i]; j++) cout << i << ' ';

}

}

排序
https://ljw030710.github.io/2023/09/26/排序/
Author
iolzyy
Posted on
September 26, 2023
Licensed under