Algorithm Note _129: Count sort (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

2.1 comparison count sort

2.2 Distribution Count sort

 1 problem description

Given a set of data, use count sorting to get this set of data from small to large sort sequences.

2 Solutions 2.1 Comparison count sort

the time complexity of the algorithm below is O (n^2), space complexity is o (n). This method is sortable for any set of data.

The specific code is as follows:

 PackageCom.liuzhen.practice; Public classMain { Public voidComparisoncountingsort (int[] A) {int[] temp =New int[A.length]; int[] Count =New int[A.length];  for(inti = 0;i < a.length-1;i++) {             for(intj = i + 1;j < a.length;j++) {                if(A[i] <A[j]) count[j]++; ElseCount[i]++; }        }         for(inti = 0;i < a.length;i++) Temp[count[i]]=A[i];  for(inti = 0;i < a.length;i++) A[i]=Temp[i]; return; }         Public Static voidMain (string[] args) {main test=NewMain (); int[] A = {2,3,1,4,6,4,3,5,3,2,5,3,5,2,3,4,5,2,54,3,21};        Test.comparisoncountingsort (A);  for(inti = 0;i < a.length;i++) System.out.print (A[i]+" "); }}

Operation Result:

2.2 Distribution count sort

the time complexity of the algorithm below is O (n), spatial complexity is o (n). The time efficiency of this method is better than that of fast sorting and merging, but this method has certain requirements for the given data, that is, the elements in the array satisfy min <= a[i] <= max, and at min ~ Max all elements between the array A have appeared in both.

The specific code is as follows:

 PackageCom.liuzhen.practice; Public classMain1 {//all of the elements in array A are greater than or equal to min, less than or equal to Max, and all min~max between them appear in array a     Public voidDistributioncountingsort (int[] A,intMinintmax) {        int[] temp =New int[A.length]; int[] D =New int[Max-min + 1];  for(inti = 0;i < a.length;i++) D[a[i]-min]++;  for(inti = 0;i < max-min;i++) D[i+ 1] = D[i + 1] +D[i];  for(inti = 0;i < a.length;i++) {            intj = A[i]-min; TEMP[D[J]-1] =A[i]; D[J]--; }         for(inti = 0;i < a.length;i++) A[i]=Temp[i]; return; }         Public Static voidMain (string[] args) {Main1 test=NewMain1 (); int[] A = {1,2,3,4,5,6,7,8,9,2,4,5,4,3,4,5,2,3,4,5,2,3,5,4,2,3,5,4,3,2,5,3,3,5}; Test.distributioncountingsort (A,1, 9);  for(inti = 0;i < a.length;i++) System.out.print (A[i]+" "); }}

Operation Result:

Resources:

1."Basic algorithm design and analysis" version 3 anany levitin Pan Yan translation

Algorithm Note _129: Count sort (Java)

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.