-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadix.java
More file actions
39 lines (31 loc) · 950 Bytes
/
Radix.java
File metadata and controls
39 lines (31 loc) · 950 Bytes
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
public class Radix {
public void countSortPorDigito(int[] array, int exp) {
int[] B = new int[array.length];
int[] C = new int[10];
for(int i = 0; i < array.length;i++) {
int digito = (array[i]/exp) % 10;
C[digito] += 1;
}
for (int i = 1; i < C.length;i++) {
C[i] += C[i-1];
}
for(int i = array.length -1; i >= 0; i--) {
int digito = (array[i]/exp) % 10;
int posicao = C[digito] -1;
B[posicao] = array[i];
C[digito]--;
}
for(int i = 0; i < B.length;i++) {
array[i] = B[i];
}
}
public void radixSort(int[] a) {
int maior = a[0];
for(int i = 0; i < a.length;i++) {
if(a[i] > maior) maior = a[i];
}
for(int exp = 1; maior/exp > 0; exp *= 10) {
countSortPorDigito(a, exp);
}
}
}