forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RadixSort.java
42 lines (33 loc) · 1.14 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
/*
* @author Marcelo Wischniowski <marcelowisc at gmail.com>
*/
public class RadixSort {
/**
* @param vetor Vetor que será ordenado
* Aplica a ordenação RadixSorte no vetor
*/
public int[] sort(int[] vetor) {
int length = vetor.length,
i,
exp = 1,
higher = vetor[0];
int[] temp = new int[10];
for (i = 1; i < length; i++)
if (vetor[i] > higher)
higher = vetor[i];
while (higher / exp > 0){
int[] bucket = new int[10];
for (i = 0; i < length; i++)
bucket[(vetor[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
bucket[i] += bucket[i - 1];
for (i = length - 1; i >= 0; i--)
temp[--bucket[(vetor[i] / exp) % 10]] = vetor[i];
for (i = 0; i < length; i++)
vetor[i] = temp[i];
exp = exp * 10;
}
return vetor;
}
}