基数排序
= = 现在才会写,感觉有点怂
就是一位一位的比较,对于每一位,使用计数排序,然后排序就好了,而且显然这是一种稳定排序算法。
#include <cstdio> #include <algorithm> using namespace std; const int maxn = 1005; int num[maxn], n, bid[maxn], cnt[maxn]; int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) scanf("%d", &num[i]); //基数排序 int *x = num, *y = bid; for (int k = 0, p = 1; k < 10; k++, p *= 10) { for (int i = 0; i < 10; i++) cnt[i] = 0; for (int i = 0; i < n; i++) { int nowval = x[i] % (p * 10) / p; cnt[nowval]++; } for (int i = 1; i < 10; i++) cnt[i] += cnt[i - 1]; for (int i = n - 1; i >= 0; i--) { int nowval = x[i] % (p * 10) / p; y[--cnt[nowval]] = x[i]; } int *t; t = x, x = y, y = t; for (int i = 0; i < n; i++) printf("%d ", x[i]); puts(""); } for (int i = 0; i < n; i++) printf("%d ", x[i]); putchar('\n'); } return 0; }