-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortStrings.java
110 lines (86 loc) · 3.31 KB
/
SortStrings.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SortStrings {
private static final String ALPHABET = "$abcdefghijklmnopqrstuvwxyz";
public static void main(String[] args) {
System.out.println("main");
String alphabet = args[0];
List<String> argsList = Arrays.asList(args);
List<String> stringsToSort = argsList.subList(1, argsList.size());
List<String> sortedStrings = SortStrings.sortStrings(alphabet, stringsToSort);
System.out.println("sorted strings:");
System.out.println(sortedStrings);
}
public static List<Integer> radixSort(List<Integer> inputList) {
List<List<Integer>> buckets = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
buckets.add(i, new LinkedList<>());
}
List<Integer> sorted = new LinkedList<>(inputList);
int currentPowerOfTen = 10;
while (currentPowerOfTen < Collections.max(inputList)) {
// put things into the buckets depending on their digit
for (int input : sorted) {
buckets.get(input % currentPowerOfTen).add(input);
}
// take things out of the buckets in order
sorted.clear();
for (int bucketNum = 0; bucketNum < 10; bucketNum++) {
sorted.addAll(buckets.get(bucketNum));
}
// move up to the next digit
currentPowerOfTen *= 10;
}
return sorted;
}
public static String convertIntegerToString(int x) {
// TODO
throw new UnsupportedOperationException();
}
public static List<String> sortStrings(String alphabet, List<String> strings) {
// Sort alphabet
// Build trie using hashing representation
// TODO: Figure out if map node or arraynode
TrieNode root = new MapNode(null, null);
for (String string : strings) {
root.addString(string);
}
// Compress trie
// Build 1 array with all nodes
// If some node v has edges "a", "c", "x", and "q",
// Add (v, "a"), (v, "c"), (v, "x"), and (v, "q") to the list
// Will need to number each node 1->n, and also number
// the letters for that node with 1->n
// For each node in trie,
// use radix sort to sort its edges
List<TrieNode> allNodes = root.getSubtree();
for (TrieNode node : allNodes) {
List<Integer> valuesToSort = new ArrayList<>();
for (String edge : node.children().stream().map(x -> x.edgeToMe).collect(Collectors.toList())) {
// Convert edge to an integer
int edgeValue = 0;
for (int i = 0; i < edge.length(); i++) {
char letter = edge.charAt(i);
int letterIndex = alphabet.indexOf(letter);
edgeValue += (int) (Math.pow(26, i) * letterIndex);
}
valuesToSort.add(edgeValue);
}
List<Integer> sortedValues = radixSort(valuesToSort);
List<String> sortedEdges = sortedValues.stream()
.map(SortStrings::convertIntegerToString)
.collect(Collectors.toList());
// TODO: Re-assign trie to use sortedEdges as children
}
// In-order traverse the trie to get the strings in sorted order
List<String> sortedStrings = new ArrayList<>();
return new ArrayList<>();
}
}