Home » Core Java » threads » Find rhyming words using QuickSort

About Ilias Tsagklis

Avatar photo
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

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.  

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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments