-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.java
More file actions
55 lines (42 loc) · 972 Bytes
/
RadixSort.java
File metadata and controls
55 lines (42 loc) · 972 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package geeks.algo.sort;
import java.util.Arrays;
public class RadixSort extends Sort {
RadixSort(int[] arr) {
super(arr);
}
@Override
protected void performSort() {
// TODO Auto-generated method stub
int maxNumber = 0;
for(int i=0;i<arr.length;i++){
if(maxNumber<this.arr[i])
maxNumber =this.arr[i];
}
int exp=1;
while(maxNumber/exp>0){
int[] count = new int[10];
Arrays.fill(count, 0);
int[] output = new int[arr.length];
for(int j=0;j<arr.length;j++){
count[arr[j]/exp%10]++;
}
for(int j=1;j<10;j++){
count[j] += count[j-1];
}
for(int j=arr.length-1; j>=0; j--){
output[count[(arr[j]/exp)%10] -1 ] =arr[j];
count[(arr[j]/exp)%10]--;
}
for(int j=0;j<arr.length;j++){
arr[j] = output[j];
}
exp = exp *10;
}
}
@Override
protected void setComplexities() {
this.timeComplexity="O(n * Log(n))";
this.spaceComplexity="N/A";
this.type="Radix Sort";
}
}