-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRadixSort.java
90 lines (80 loc) · 2.99 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
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
import java.util.ArrayList;
/**
* Class for doing Radix sort
*
* @author Akhil Batra, Alexander Hwang
*/
public class RadixSort {
/**
* Does LSD radix sort on the passed in array with the following restrictions:
* The array can only have ASCII Strings (sequence of 1 byte characters)
* The sorting is stable and non-destructive
* The Strings can be variable length (all Strings are not constrained to 1 length)
*
* @param asciis String[] that needs to be sorted
* @return String[] the sorted array
*/
public static String[] sort(String[] asciis) {
int numDigitsInAnInteger = 0;
for (int i = 0; i < asciis.length; i++) {
if (asciis[i].length() > numDigitsInAnInteger) {
numDigitsInAnInteger = asciis[i].length();
}
}
String[] sortedAsciis = asciis;
for (int i = 0; i < numDigitsInAnInteger; i++) {
sortedAsciis = sortHelperLSD(sortedAsciis, i, numDigitsInAnInteger);
}
// print(asciis);
// print(sortedAsciis);
return sortedAsciis;
}
private static void print(String[] strings) {
for (int i = 0; i < strings.length; i++) {
System.out.print(strings[i] + ' ');
}
System.out.println();
}
/**
* LSD helper method that performs a destructive counting sort the array of
* Strings based off characters at a specific index.
*
* @param asciis Input array of Strings
* @param index The position to sort the Strings on.
*/
private static String[] sortHelperLSD(String[] asciis, int index, int numDigitsInAnInteger) {
ArrayList<String>[] buckets = new ArrayList[256];
for (int i = 0; i < 256; i++) {
buckets[i] = new ArrayList<>();
}
for (String str : asciis) {
int charIndex;
if (numDigitsInAnInteger - index > str.length()) {
charIndex = 0;
} else {
charIndex = (int) str.charAt(numDigitsInAnInteger - index - 1);
}
buckets[charIndex].add(str);
}
int currentIndex = 0;
for (ArrayList<String> bucket : buckets) {
for (String str : bucket) {
asciis[currentIndex++] = str;
}
}
return asciis;
}
/**
* MSD radix sort helper function that recursively calls itself to achieve the sorted array.
* Destructive method that changes the passed in array, asciis.
*
* @param asciis String[] to be sorted
* @param start int for where to start sorting in this method (includes String at start)
* @param end int for where to end sorting in this method (does not include String at end)
* @param index the index of the character the method is currently sorting on
**/
private static void sortHelperMSD(String[] asciis, int start, int end, int index) {
// Optional MSD helper method for optional MSD radix sort
return;
}
}