Good, better, best. Never let it rest.

Java语言实现计数排序

粘贴代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
计数排序:
public static void countSort(int[] arr) {
int[] result = new int[arr.length];
int[] count = new int[6]; // 数组大小为原数组最大元素大小+1

for (int i=0; i<arr.length; i++) {
count[arr[i]]++;
}

for (int i = 0, j=0; i < count.length; i++) {
while (count[i]-- >0) result[j++] = i;
}
}