Java implementation count ordering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.