快排
void quick_sort(int q[], int l, int r) {
if (l >= r) return;
int i = l - 1, j = r + 1, x = q[l + r >> 1];
while (i < j) {
do i++; while (q[i] < x);
do j--; while (q[j] > x);
if (i < j) swap (q[i], q[j]);
}
quick_sort(q, l, j), quick_sort(q, j + 1, r);
}
https://www.acwing.com/problem/content/787/
快排详解:https://blog.csdn.net/u013570834/article/details/106183702
归并
void merge_sort(int q[], int l, int r) {
if (l >= r) return;
int m = l + r >> 1, i = l, j = m + 1, k = 0;
merge_sort(q, l, m), merge_sort(q, m + 1, r);
while (i <= m && j <= r) tmp[k++] = q[i] < q[j] ? q[i++]:q[j++];
while (i <= m) tmp[k++] = q[i++];
while (j <= r) tmp[k++] = q[j++];
for (i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];
}
https://www.acwing.com/problem/content/789/
归并详解 https://blog.csdn.net/u013570834/article/details/106165046
堆排
void down(int u) {
int t = u;
if (2 * u <= h[0] && h[t] > h[2 * u]) t = 2 * u;
if (2 * u + 1 <= h[0] && h[t] > h[2 * u + 1]) t = 2 * u + 1;
if (u != t) {
swap(h[u], h[t]);
down(t);
}
}
void heap_sort(int a[], int n) {
for (int i = 1; i <= n; i++) h[i] = a[i - 1];
h[0] = n;
for (int i = n / 2; i; i--) down(i);
for (int i = 0; i < n; i++) {
a[i] = h[1];
h[1] = h[h[0]--];
down(1);
}
}
https://www.acwing.com/problem/content/description/840/