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