See "Introduction to Algorithms" in detail in section 8.2-counting sequencing.
The Java code is as follows:
Package linetimesort;/** * Count ordering assuming that each of the N input elements is an integer between 0 and K, K is an integer; Determines where the number should be placed by determining the number of less than or equal to one number of points * @author Yuncong * */public class Countsort {/** * * @param an array before sort * @param b sorted array * @param k the value of the largest element in the sorted array */public void sort (int[] A, int[] b, int k) {//create array C, and initialize int[] c = new Int[k + 1];for (int i = 0; i < c.length; i++) {c[i] = 0;} Counts the number of occurrences of each element in array a for (int i = 0; i < a.length; i++) {c[a[i]]++;} /** * The number of numbers in the array A that is less than or equal to a number; * Because the number of numbers less than or equal to 0 is the number that equals 0, the iteration starts at 1 */for (int i = 1; i < c.length; i++) {c[i] = C[i] + c[i-1];} for (int i = 0; i < a.length; i++) {/** * is less than or equal to A[i] The number of numbers is x it should be placed in the first x-1 position of array B, * because the array subscript starts from 0 */b[c[a[i]] 1] = a[i];/** * Next A[i] in front of this a[i]; * Next A[i] is preceded by a space */c[a[i]]--in front for all numbers less than or equal to a[i] sufficient;}} public static void Main (string[] args) {int[] a = new int[]{3, 1, 5, 6};int[] b = new int[5];new Countsort (). Sort (A, B for (int i = 0; i < b.length; i++) {System.out.println (b[i]);}}}
Java implementation count ordering