Algorithm-java code implementation cardinality sort

Source: Internet
Author: User

Base sort 11th Section cardinal sort Exercises

For an int array, write a cardinal sort algorithm that sorts the array elements.

Given an int array a and the size of the array n, return the sorted array. The guaranteed element is less than or equal to 2000.

Test examples:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
Java (javac 1.7)Code Auto-Complete1
Import Java. util. *;
2
3
 Public class Radixsort {
4
   Everyone, the method of installation
5
     Public int Radixsort (intAintn) {
6
        int length = n;
7
        int Divisor = 1; //define the divisor of each round, 1,10,100 ...
8
9
        int Buckets = New int [Ten] [length]; //defined 10 barrels, in case each one is the same all put in a bucket
10
        int Count = New int [ten]; //Count the number of elements actually stored in each bucket
11
        int Digit; //Gets the number on the corresponding bit in the element, which is loaded into the bucket
12
13
         for (inti=1i<=3i+ +) {//After 4-pass operation, sorting complete
14
             for (inttempA) {//calculate into bucket
15
                Digit = (temp/divisor%;
16
                Buckets [Digit] [count[digit]+ +=temp;
17
            }
18
19
            int k = 0; //subscript of sorted array
20
             for (intb=0b<b+ +) {//from 0 to 9th barrels taken in sequence
21st
                if (count[b= =0)//If no elements in this bucket are placed, skip
22
                    continue;
23
                 for (intw=0w<count[bw  ++) {
24
                    A [k+ +=bucket[b] [w];
25
                }
26
                Count [b=0; //The elements in the bucket have all been taken out and the counter zeroed
27
            }
28
            Divisor *= ten;
29
        }
30
        return A;
31
    }
32
}
Your code is saved
The answer is right: Congratulations! The program you submitted passed all of the test cases

Algorithm-java code implementation cardinality sort

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.