-
Notifications
You must be signed in to change notification settings - Fork 0
/
Buckets.java
78 lines (60 loc) · 1.9 KB
/
Buckets.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
import java.util.*;
public class Buckets {
private List<String> zeroBucket;
private List<String> oneBucket;
public Buckets(){
zeroBucket = new ArrayList<>();
oneBucket = new ArrayList<>();
}
public void addZero(String newValue){
zeroBucket.add(newValue);
}
public void addOne(String newValue){
oneBucket.add(newValue);
}
public List<String> getAllInOrder(){
List<String> result = new ArrayList<>();
for(String val: oneBucket){
result.add(val);
}
for(String val: zeroBucket){
result.add(val);
}
return result;
}
public void print(List<String> T){
for(String num: T){
System.out.print(num + " ");
}
System.out.println("");
}
public static void main(String[] args) {
String[] origNumbers = {"0011", "1001","1000","0111","0101"};
List<String> numbers = new ArrayList<>();
for(String num: origNumbers){
numbers.add(num);
}
int numDigits = 4;
sort(numbers, numDigits);
}
public static List<String> sort(List<String> bin, int n){
Buckets buckO = new Buckets();
List<String> tmp = new ArrayList<>(bin);
buckO.print(tmp);
for(int i = 0; i < n; i++){
Buckets buck = new Buckets();
for(int j = 0; j < tmp.size(); j++){
String val = tmp.get(j);
char c = val.charAt((n-1)-i);
if(c == '1'){
buck.addOne(val);
}else if(c == '0'){
buck.addZero(val);
}
}
buck.print(buck.getAllInOrder());
tmp = new ArrayList<>(buck.getAllInOrder());
}
return null;
}
}