Package WZS. sort; import Java. util. arrays; // count sorting // find the largest and smallest elements in the array to be sorted // count the number of times each element with a value of I appears in the array, store the I entry of array C // accumulate all the counts (starting from the first element in C, each of which is added to the previous one) // reversely fill the target array: place each element I in item C (I) of the new array) minus 1 public class countingsort {public static void main (string [] argv) {int [] A = countingsort. countingsort (New int [] {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}); system. out. println (arrays. tostring (a);} public static int [] countingsort (INT [] A) {int [] B = new int [. length]; // assume that data a' in a has, 0 <= A' & a' <K and K = 100 int K = 100; countingsort (A, B, k); return B;} Private Static void countingsort (INT [] A, int [] B, int K) {int [] C = new int [k]; // count for (Int J = 0; j <. length; j ++) {int A = A [J]; C [a] + = 1 ;}// calculate the count and for (INT I = 1; I <K; I ++) {C [I] = C [I] + C [I-1];} // sort for (Int J =. length-1; j> = 0; j --) {int A = A [J]; B [C [a]-1] =; c [a]-= 1 ;}}}
Output result:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]