-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRadixSort.java
87 lines (78 loc) · 1.94 KB
/
RadixSort.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Copyright 2022 jingedawang
*/
package sort;
import utils.ArrayPrinter;
import utils.ArrayGenerator;
/**
* Radix sort algorithm.
*/
public class RadixSort implements Sort {
/**
* Demo code.
*/
public static void main(String[] args) {
// The maximum number of digits of the elements.
int d = 3;
// The upper limit of each digit.
int k = 10;
int[] arr = ArrayGenerator.randomArray((int) Math.pow(k, d));
System.out.println("Original array:");
ArrayPrinter.print(arr);
Sort sort = new RadixSort(d, k);
sort.sort(arr);
System.out.println();
System.out.println("Sorted by radix sort:");
ArrayPrinter.print(arr);
}
/**
* Constructor with {@code d} and {@code k} specified.
*
* @param d The maximum number of digits of the elements.
* @param k The upper limit of each digit.
*/
public RadixSort(int d, int k) {
this.d = d;
this.k = k;
}
/**
* Radix sort.
*
* @param arr Integer array to be sorted.
*/
@Override
public void sort(int[] arr) {
radixSort(arr, d, k);
}
/**
* Radix sort an array.
*
* @param arr The array to be sorted.
* @param d The maximum number of digits of the elements.
* @param k The upper limit of each digit.
*/
public void radixSort(int[] arr, int d, int k) {
int[] digitArr = new int[arr.length];
CountingSort countingSort = new CountingSort(k);
for (int i = 0; i < d; i++) {
for (int j = 0; j < arr.length; j++) {
digitArr[j] = arr[j];
for (int l = 0; l < i; l++) {
digitArr[j] /= k;
}
digitArr[j] %= k;
}
int[] indices = new int[digitArr.length];
countingSort.countingSortWithIndices(digitArr, new int[digitArr.length], k, indices);
int[] temp = new int[arr.length];
for (int j = 0; j < arr.length; j++) {
temp[j] = arr[indices[j]];
}
System.arraycopy(temp, 0, arr, 0, temp.length);
}
}
// The maximum number of digits of the elements.
private final int d;
// The upper limit of each digit.
private final int k;
}