Java Sorting Algorithm _ 005 count sorting

Source: Internet
Author: User
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]

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.