forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLSD.java
More file actions
69 lines (63 loc) · 1.39 KB
/
LSD.java
File metadata and controls
69 lines (63 loc) · 1.39 KB
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
package chap5;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class LSD
{
private String[] a;
private int lengthOfChars;
private static int R = 256;
public LSD(String path) throws FileNotFoundException
{
List<String> tempList = new ArrayList<String>();
Scanner scanner = new Scanner(new File(path));
// scanner.useDelimiter(" ");
while(scanner.hasNext())
{
tempList.add(scanner.next());
}
this.a = tempList.toArray(new String[]{});
this.lengthOfChars = this.a[0].length();
}
public void process()
{
LSD.sort(a, this.lengthOfChars);
}
public static void sort(String[] a, int lengthOfChars)
{
String[] aux = new String[a.length];
// int[] count = new int[R + 1];
// Outmost Loop
for(int i = lengthOfChars - 1; i >= 0; i--)
{
// Every loop should re-create the count array
int[] count = new int[R + 1];
// Compute the frequency
for(int j = 0; j < a.length; j++)
{
count[a[j].charAt(i) + 1]++;
}
// Transform counts to indices
for(int j = 0; j < R; j++)
{
count[j + 1] += count[j];
}
// Distribute the records
for(int j = 0; j < a.length; j++)
{
aux[count[a[j].charAt(i)]++] = a[j];
}
// Copy back
for(int j = 0; j < a.length; j++)
{
a[j] = aux[j];
}
}
}
public String[] getStrings()
{
return this.a;
}
}