public class CountingSort {
int[] sort() {
int N = 10000;
int M = 100;//假设A中最大值为100
int A[] = new int[N];//1~k,原数组
int B[] = new int[N];//1~k,Stores incompletely sorted numbers
int C[] = new int[M + 1];//0~n,辅助排序数组
for (int i = 0; i <= M; i++) {
C[i] = 0;
}
for (int i = 0; i < A.length; i++) {
C[A[i]] += 1;
}
//统计<=i的个数
for (int i = 0; i <= M; i++) {
C[i] = C[i] + C[i - 1];
}
for (int i = N; i >= 1; i--) {
B[C[A[i]]] = A[i];
C[A[i]] -= 1;
}
return B;
}
}
C告诉B应该把A排在哪里,每排一个数字,都要把相应<=的个数减1