0

This is my code so far. I need help to implement a main method that reads and sorts the supplied test files(unsorted1.txt and unsorted2.txt)

public class quickSort extends DLList  {    
 public static <E extends Comparable <? super E>> void quickSort(DLList<E> element){
    sort(element, 0, element.size() - 1); 
 }

 public static <E extends Comparable <? super E>> void sort(DLList<E> element, int l, int r) {
    int i = l;
    int j = r;
    E pivot = element.get((l + r) / 2), w;
    do {
        while (element.get(i).compareTo(pivot)< 0){ 
            ++i;
        }
        while (element.get(j).compareTo(pivot)> 0){ 
            --j;
        }
        while (i <= j) {
            w = element.get(i);
            element.set(i, element.get(j));
            element.set(j, w);
            ++i;
            --j;
        }
    } while (i <= j);

    if (l < j) {
        sort(element, l, j);
    }

    if (i < r) {
        sort(element, i, r);
    }
}

    public static void main(String[] args){

    }

My quicksort implementation is complete and it is based on a doubly linked list. The text files contain a number of unsorted characters. So I have to load all characters and store them in the list. And that's what I need help with.

ChrisF
  • 127,439
  • 29
  • 243
  • 315

1 Answers1

0

Can you please elaborate a little more on files part?

What is the data format in the file? Should the output format after sorting be the same as input format?

As much as i understand your quicksort implementation is ready, all that you need is to parse the text file and sort and write it back to the file.

zerocool
  • 1,927
  • 10
  • 14
  • Okie. So all that you need now is to read the text file character by character. Make use of BufferedReader, http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html#read() . Once you have each character you can add these one by one to the linked list. and finally pass this linked list to your sorting api – zerocool Jun 12 '13 at 11:14
  • You mean something like this? public class Main { public static void main(String[] args) throws Exception { File file = new File("test.txt"); StringBuffer contents = new StringBuffer(); BufferedReader reader = null; reader = new BufferedReader(new FileReader(file)); String text = null; while ((text = reader.readLine()) != null) { contents.append(text).append(System.getProperty("line.separator")); } reader.close(); System.out.println(contents.toString()); } } – user2474434 Jun 12 '13 at 12:15
  • I have few more questions to ask you: I am still not clear with the content of the file. When you say you have few characters what do you mean? Is it just a word(String) e.g. "ABCD" or it is: A B C D?Can you share your file? because sorting sentence is different than sorting characters. And in either cases, why dont you use java.util.List (any implementation be it java.util.LinkedList or java.util.ArrayList) of String or Character, and call Collections.sort on it? – zerocool Jun 12 '13 at 14:13
  • The idea is that the main method must read in my text file containing unsorted letters (mnsovx. .. etc). And the task is to test how fast quicksort sorts the unsorted letters. There are 3 text files to be uploaded, and each contains a different amount of letters. – user2474434 Jun 13 '13 at 11:19