001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
package algs51; // section 5.1
import stdlib.*;
/* *********************************************************************************
* Compilation: javac MSD.java
* Execution: java MSD < input.txt
*
* Reads extended ASCII string from standard input and MSD radix sorts them.
*
* % java MSD < shells.txt
* are
* by
* sea
* seashells
* seashells
* sells
* sells
* she
* she
* shells
* shore
* surely
* the
* the
*
* MAX_LENGTH = 64
* RESULTS WITH Arrays.sort (Mergesort)
*
* $ ./runjava algs51.MSD
* 256000 [0.243 0.880]
* 512000 [0.398 1.638]
* 1024000 [0.851 2.138]
* $ ./runjava algs51.MSD
* 256000 [0.281 1.011]
* 512000 [0.365 1.299]
* 1024000 [0.848 2.323]
* $ ./runjava algs51.MSD
* 256000 [0.270 1.031]
* 512000 [0.370 1.370]
* 1024000 [0.857 2.316]
*
* RESULTS with MSD
*
* $ ./runjava algs51.MSD
* 256000 [0.299 3.785]
* 512000 [0.151 0.505]
* 1024000 [0.305 2.020]
* $ ./runjava algs51.MSD
* 256000 [0.296 3.795]
* 512000 [0.139 0.470]
* 1024000 [0.303 2.180]
* $ ./runjava algs51.MSD
* 256000 [0.294 3.769]
* 512000 [0.141 0.480]
* 1024000 [0.282 2.000]
*
* RESULTS WITH Arrays.sort (Mergesort)
*
* $ ./runjava algs51.MSD
* dickens: [4.205]
* $ ./runjava algs51.MSD
* dickens: [4.257]
* $ ./runjava algs51.MSD
* dickens: [4.222]
*
* RESULTS with MSD
*
* $ ./runjava algs51.MSD
* dickens: [7.331]
* $ ./runjava algs51.MSD
* dickens: [7.411]
* $ ./runjava algs51.MSD
* dickens: [7.523]
*
***********************************************************************************/
public class MSD {
private static final int R = 256; // extended ASCII alphabet size
private static final int CUTOFF = 15; // cutoff to insertion sort
// sort array of strings
public static void sort(String[] a) {
int N = a.length;
String[] aux = new String[N];
sort(a, 0, N-1, 0, aux);
}
// return dth character of s, -1 if d = length of string
private static int charAt(String s, int d) {
assert d >= 0 && d <= s.length();
if (d == s.length()) return -1;
return s.charAt(d);
}
// sort from a[lo] to a[hi], starting at the dth character
private static void sort(String[] a, int lo, int hi, int d, String[] aux) {
// cutoff to insertion sort for small subarrays
if (hi <= lo + CUTOFF) {
insertion(a, lo, hi, d);
return;
}
// compute frequency counts
int[] count = new int[R+2];
for (int i = lo; i <= hi; i++) {
int c = charAt(a[i], d);
count[c+2]++;
}
// transform counts to indicies
for (int r = 0; r < R+1; r++)
count[r+1] += count[r];
// distribute
for (int i = lo; i <= hi; i++) {
int c = charAt(a[i], d);
aux[count[c+1]++] = a[i];
}
// copy back
for (int i = lo; i <= hi; i++)
a[i] = aux[i - lo];
// recursively sort for each character
for (int r = 0; r < R; r++) {
//sort(a, lo + count[r], lo + count[r+1] - 1, d+1, aux);
int newLo = lo + count[r];
int newHi = lo + count[r+1] - 1;
if (newHi > newLo)
sort(a, newLo, newHi, d+1, aux);
}
}
// return dth character of s, -1 if d = length of string
private static void insertion(String[] a, int lo, int hi, int d) {
for (int i = lo; i <= hi; i++)
for (int j = i; j > lo && less(a[j], a[j-1], d); j--)
exch(a, j, j-1);
}
// exchange a[i] and a[j]
private static void exch(String[] a, int i, int j) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
// is v less than w, starting at character d
private static boolean less1(String v, String w, int d) {
assert v.substring(0, d).equals(w.substring(0, d));
return v.substring(d).compareTo(w.substring(d)) < 0;
}
// is v less than w, starting at character d
private static boolean less(String v, String w, int d) {
assert v.substring(0, d).equals(w.substring(0, d));
int len1 = v.length ();
int len2 = w.length ();
int n = Math.min(len1, len2);
int k = 0;
while (k < n) {
char c1 = v.charAt (k);
char c2 = w.charAt (k);
if (c1 != c2) {
return c1 < c2;
}
k++;
}
return len1 < len2;
}
/* *********************************************************************************
* Test code
***********************************************************************************/
private static final int MAX_LENGTH = 64;
private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`~!@#$%^&*()_-+={[}]\\|:;'\"<>,./?";
private static String randomString() {
int length = 2 + StdRandom.uniform (MAX_LENGTH);
char[] text = new char[length];
int NUM_CHARACTERS = CHARACTERS.length ();
for (int i = 0; i < length; i++) {
text[i] = CHARACTERS.charAt(StdRandom.uniform (NUM_CHARACTERS));
}
return new String(text);
}
private static void exch(int[] a, int i, int j) {
int swap = a[i];
a[i] = a[j];
a[j] = swap;
}
private static double time;
private static void countops (int N) {
String[] a = new String[N];
//for (int i = 0; i < a.length; i++) a[i] = (N*i)/N;
//StdRandom.shuffle (a);
for (int i = 0; i < a.length; i++) a[i] = randomString ();
//Arrays.sort (a);
//Arrays.sort (a); for (int i = 0; i < (N-1)/2; i++) exch(a, i, N-i-1);
Stopwatch sw = new Stopwatch ();
sort (a);
//Quick3way.sort (a);
//Arrays.sort (a);
time = sw.elapsedTime ();
}
public static void main(String[] args) {
int N = 128000;
countops (N);
//System.exit (0);
double prevTime = time;
for (int i=0; i<3; i++) {
N *= 2;
countops (N);
StdOut.format("%8d [%5.3f %5.3f]\n", N, time, time/prevTime);
prevTime = time;
}
}
public static void main3(String[] args) {
String[] a = new In("data/dickens.txt").readAllStrings ();
Stopwatch sw = new Stopwatch ();
sort (a);
//Arrays.sort (a);
StdOut.format("dickens: [%5.3f]\n", sw.elapsedTime ());
}
public static void main2(String[] args) {
String[] a = StdIn.readAllStrings();
int N = a.length;
sort(a);
for (int i = 0; i < N; i++)
StdOut.println(a[i]);
}
}
|