计数排序的思想是基于桶排序,时间复杂度趋近去O(n), 空间复杂度为O(m)(最大最小差值)。
public class CountingSort {
public int[] countingSort(int[] A, int n) {
// write code here
int max = A[0];
int min = A[0];
for(int i =0; i <n; i++){
if(A[i]> max) max = A[i];
if(A[i]< min) min = A[i];
}
int []B = new int[max-min+1];
for(int i = 0; i<n; i++){
B[A[i] - min]++;
}
int index = 0;
for(int i = 0; i<B.length; i++){
while(--B[i]>=0){
A[index++] = i+min;
}
}
return A;
}
}