threads

Find rhyming words using QuickSort

In this example we shall show you how to find rhyming words using the QuickSort algorithm. We have created two classes, SortThread and ReverseThread that both extend the Thread and override the run() method of Thread. The two threads and the example are described in short:

  • In their run() methods the SortThread calls its private quicksort(String[] str, int low0, int high0) method that implements the QuickSort algorithm to sort a String array, whereas the ReverseThread calls its reverseIt(String source) method that reverses a String.
  • We read a file, using a FileReader for a specified file path.
  • We use reverseOrder(Reader src) and then sort(Reader src) and then reverseOrder(Reader src) methods for the fileReader.The result is a Reader, that reads character streams.
  • In the reverseOrder(Reader src) method, the reader is put in a PipedReader through a BufferedReader and then the ReverseThread is called for execution to reverse the PipedReader and put it in the PipedWriter.
  • In the sort(Reader src) method it puts the Reader in a PipedReader through a BufferedReader and then the SortThread is called for execution to sort the PipedReader and put it in the PipedWriter,

as described in the code snippet below.  

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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package com.javacodegeeks.snippets.core;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
import java.io.Reader;
 
 
public class RhymingWords {
 
    public static void main(String[] args) throws IOException {
 
 
  FileReader wordFile = new FileReader("C:/Users/nikos7/Desktop/words.txt");
 
 
  Reader rhymingwords = reverseOrder(sort(reverseOrder(wordFile)));
 
 
  BufferedReader inp = new BufferedReader(rhymingwords);
 
  String input;
 
 
  while ((input = inp.readLine()) != null) {
 
 
System.out.println(input);
 
  }
 
   
 
  inp.close();
    }
 
    public static Reader reverseOrder(Reader src) throws IOException {
 
 
  BufferedReader input = new BufferedReader(src);
 
 
  PipedWriter pipeOut = new PipedWriter();
 
  PipedReader pipeIn = new PipedReader(pipeOut);
 
  PrintWriter out = new PrintWriter(pipeOut);
 
 
  new ReverseThread(out, input).start();
 
 
  return pipeIn;
    }
 
    public static Reader sort(Reader src) throws IOException {
 
 
  BufferedReader input = new BufferedReader(src);
 
 
  PipedWriter pipeOut = new PipedWriter();
 
  PipedReader pipeIn = new PipedReader(pipeOut);
 
  PrintWriter out = new PrintWriter(pipeOut);
 
 
  new SortThread(out, input).start();
 
 
  return pipeIn;
    }
}
class SortThread extends Thread {
 
    private PrintWriter output = null;
    private BufferedReader input = null;
 
    public SortThread(PrintWriter output, BufferedReader input) {
 
  this.output = output;
 
  this.input = input;
    }
 
    @Override
    public void run() {
 
  int MAXWORDS = 250;
 
 
  if (output != null && input != null) {
 
 
try {
 
 
    String[] words = new String[MAXWORDS];
 
 
    int numwords = 0;
 
 
 
    while ((words[numwords] = input.readLine()) != null) {
 
 
 
  numwords++;
 
 
    }
 
 
    quicksort(words, 0, numwords - 1);
 
 
    for (int i = 0; i < numwords; i++) {
 
 
 
  output.println(words[i]);
 
 
    }
 
 
    output.close();
 
 
} catch (IOException e) {
 
 
     
 
 
    System.err.println("Sort: " + e);
 
 
}
 
  }
    }
 
    private static void quicksort(String[] str, int low0, int high0) {
 
  int loPart = low0;
 
  int hiPart = high0;
 
 
  if (loPart >= hiPart) {
 
 
return;
 
  }
 
 
  String middle = str[(loPart + hiPart) / 2];
 
   
 
  while (loPart < hiPart) {
 
 
 
 
 
while (loPart < hiPart && str[loPart].compareTo(middle) < 0) {
 
 
    loPart++;
 
 
}
 
 
 
 
 
while (loPart < hiPart && str[hiPart].compareTo(middle) > 0) {
 
 
    hiPart--;
 
 
}
 
 
 
 
 
if (loPart < hiPart) {
 
 
     
 
 
    String T = str[loPart];
 
 
    str[loPart] = str[hiPart];
 
 
    str[hiPart] = T;
 
 
    loPart++;
 
 
    hiPart--;
 
 
}
 
  }
 
  if (hiPart < loPart) {
 
 
int T = hiPart;
 
 
hiPart = loPart;
 
 
loPart = T;
 
  }
 
   
 
  quicksort(str, low0, loPart);
 
  quicksort(str, loPart == low0 ? loPart + 1 : loPart, high0);
    }
}
 
class ReverseThread extends Thread {
 
    private PrintWriter output = null;
    private BufferedReader inputre = null;
 
    public ReverseThread(PrintWriter out, BufferedReader in) {
 
  this.output = out;
 
  this.inputre = in;
    }
 
    @Override
    public void run() {
 
  if (output != null && inputre != null) {
 
 
try {
 
 
    String input;
 
 
    while ((input = inputre.readLine()) != null) {
 
 
 
  output.println(reverseIt(input));
 
 
 
  output.flush();
 
 
    }
 
 
    output.close();
 
 
} catch (IOException e) {
 
 
    System.err.println("Reverse: " + e);
 
 
}
 
  }
    }
 
    private String reverseIt(String source) {
 
   
 
  int it, len = source.length();
 
  StringBuffer target = new StringBuffer(len);
 
 
  for (it = (len - 1); it >= 0; it--) {
 
 
target.append(source.charAt(it));
 
  }
 
  return target.toString();
    }
}

  
This was an example of how to rhyming words using the QuickSort algorithm in Java.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button