计数排序
public class CountSort {
public static void countSort(int[] arr
) {
if (arr
== null
|| arr
.length
< 2) {
return;
}
int max
= Integer
.MIN_VALUE
;
for (int i
= 0; i
< arr
.length
; i
++) {
max
= Math
.max(arr
[i
], max
);
}
int[] bucket
= new int[max
+ 1];
for (int i
= 0; i
< bucket
.length
; i
++) {
bucket
[arr
[i
]]++;
}
int i
= 0;
for (int j
= 0; j
< bucket
.length
; j
++) {
while (bucket
[j
]-- > 0) {
arr
[i
++] = j
;
}
}
}
public static void main(String
[] args
) {
int[] arr
= { 2, 3, 6, 1, 8, 5, 5, 6, 0 };
countSort(arr
);
for (int i
= 0; i
< arr
.length
; i
++) {
System
.out
.println(arr
[i
]);
}
}
}
转载请注明原文地址: https://mac.8miu.com/read-503483.html