冒泡排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = n - 1; i > 0; i --) {
for (int j = 1; j <= i; j ++) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
}
}
}
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
选择排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = n; i > 1; i --) {
int t = 1;
for (int j = 2; j <= i; j ++) {
if (a[j] > a[t]) {
t = j;
}
}
swap(a[t], a[i]);
}
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
插入排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
int j = i;
while (j > 1 && a[j] < a[j - 1]) {
swap(a[j], a[j - 1]);
j --;
}
}
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
计数排序
#include
using namespace std;
const int N = 1009;
int n, m, x, a[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
if (!a[x]) {
m ++;
a[x] ++;
}
}
cout << m << endl;
for (int i = 1; i <= 1000; i ++) {
if (a[i]) {
cout << i << ' ';
}
}
return 0;
}
归并排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N];
void mergesort(int l, int r) {
if (l == r) return;
int mid = l + r >> 1;
mergesort(l, mid);
mergesort(mid + 1, r);
int i = l, j = mid + 1, k = l;
while (i <= mid && j <= r) {
if (a[i] <= a[j]) {
b[k ++] = a[i ++];
}
else {
b[k ++] = a[j ++];
}
}
while (i <= mid) {
b[k ++] = a[i ++];
}
while (j <= r) {
b[k ++] = a[j ++];
}
for (i = l; i <= r; i ++) a[i] = b[i];
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
mergesort(1, n);
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
快速排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N];
void quicksort(int l, int r) {
if (l == r) return;
int x = a[l + r >> 1];
int i = l - 1, j = r + 1;
while (i < j) {
do {
i ++;
}while (a[i] < x);
do {
j --;
}while (a[j] > x);
if (i < j) {
swap(a[i], a[j]);
}
}
quicksort(l, j);
quicksort(j + 1, r);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
quicksort(1, n);
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
堆排序
#include
using namespace std;
const int N = 109;
int n, m, a[N], b[N], cnt;
void down(int u) {
int maxx = u;
if (u * 2 <= cnt && a[u * 2] > a[maxx]) maxx = 2 * u;
if (u * 2 + 1 <= cnt && a[u * 2 + 1] > a[maxx]) maxx = 2 * u + 1;
if (maxx != u) {
swap(a[maxx], a[u]);
down(maxx);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
cnt = n;
for (int i = n / 2; i > 0; i --) down(i);
for (int i = 1; i <= n; i ++) {
swap(a[1], a[cnt --]);
down(1);
}
b[1] = a[1];
m = 1;
for (int i = 2; i <= n; i ++) {
while (a[i] == b[m]) i ++;
b[++ m] = a[i];
}
cout << m << endl;
for (int i = 1; i <= m; i ++) cout << b[i] << ' ';
return 0;
}
二叉排序树
#include
using namespace std;
const int N = 109;
int n, x, idx, root;
struct BST {
int l, r, val;
}tr[N];
void insert(int& u, int x) {
if (!u) {
u = ++ idx;
tr[u].val = x;
return;
}
if (x > tr[u].val) insert(tr[u].r, x);
else if (x < tr[u].val) insert(tr[u].l, x);
}
void inorder(int u) {
if (!u) return;
inorder(tr[u].l);
cout << tr[u].val << ' ';
inorder(tr[u].r);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(root, x);
}
cout << idx << endl;
inorder(root);
return 0;
}
字典树
#include
using namespace std;
const int N = 109;
int n, m, x, idx = 1, son[1100][2], h[1100];
void insert(int x) {
int u = 1;
for (int i = 9; i >= 0; i --) {
int j = x >> i & 1;
if (!son[u][j]) son[u][j] = ++ idx;
u = son[u][j];
}
if (!h[u]) {
m ++;
h[u] = x;
}
}
void dfs(int u) {
if (h[u]) {
cout << h[u] << ' ';
return;
}
if (son[u][0]) dfs(son[u][0]);
if (son[u][1]) dfs(son[u][1]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
insert(x);
}
cout << m << endl;
dfs(1);
return 0;
}
set
#include
using namespace std;
const int N = 109;
int n, x;
set <int> s;
set <int> :: iterator it;
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) {
cin >> x;
s.insert(x);
}
cout << s.size() << endl;
for (it = s.begin(); it != s.end(); it ++) {
cout << *it << ' ';
}
return 0;
}
sort + unique
#include
using namespace std;
const int N = 109;
int n, m, a[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
sort(a + 1, a + 1 + n);
m = unique(a + 1, a + 1 + n) - &a[1];
cout << m << endl;
for (int i = 1; i <= m; i ++) {
cout << a[i] << ' ';
}
return 0;
}