-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
53 lines (37 loc) · 1.33 KB
/
RadixSort.java
File metadata and controls
53 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package kb.sort;
import java.util.Arrays;
import kb.sort.api.Sortable;
public class RadixSort implements Sortable {
@Override
public int[] sort(int[] nums) {
int max = 0;
for (int num : nums)
if (num > -1)
max = Math.max(num, max);
else
throw new UnsupportedOperationException("Negative numbers are not supported.");
for (int exp = 1; max / exp > 0; exp *= 10)
nums = countSort(nums, exp);
return nums;
}
public int[] countSort(int[] nums, int exp) {
int[] counts = new int[10];
for (int num : nums)
counts[(num / exp) % 10]++;
for (int i = 1; i < counts.length; i++)
counts[i] += counts[i - 1];
int[] output = new int[nums.length];
for (int i = nums.length - 1; i >= 0; i--) {
output[counts[(nums[i] / exp) % 10] - 1] = nums[i];
counts[(nums[i] / exp) % 10]--;
}
return output;
}
public static void main(String[] args) {
Sortable rSort = new RadixSort();
int[] arr = { 1, 4, 1, 2, 7, 5, 2 };
System.out.println(Arrays.toString(rSort.sort(arr)));
int[] arr1 = { 9, 10, 1, 0, 20, 6, 5, 4, 1, 3, 3, 2, 0, 7, 9, 8 };
System.out.println(Arrays.toString(rSort.sort(arr1)));
}
}