English 中文(简体)
印刷步骤
原标题:Print steps of sort
  • 时间:2012-04-19 09:46:51
  •  标签:
  • java
  • sorting

i was wondering of it is possible to print out every step of the sort. Here is my code: as you can see, i pass in a linked list, the values of i copy to an array to possibly make my sorting life easier and then write the sorted array to a separate text file.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Insertion 
{
    public void Sort (LinkedList listIn, int size) throws Exception
    {
        String[] insArray = new String[size] ;
        String textContent = null ;
        File outputFile ;

        //copy the list values in the array
        for (int i = 0 ; i < size ; i++)
        {
             insArray [i] = listIn.get(i).printNode();
        }

        Stopwatch timer = new Stopwatch().start(); 

        //Insertion Sort
        for (int i = 1; i < size; i++)
            for (int j = i; j > 0; j--)
            {
                if (insArray[j-1].compareToIgnoreCase(insArray[j]) > 0)
                {
                    replace(insArray, j, j-1);

                }
            }

         timer.stop();   

         do
            {
                outputFile = new File("[Insertion] Sorted Entries.txt") ;

                    if(!outputFile.exists())
                    {
                        outputFile.createNewFile ();                    
                        System.out.println("Sorted file created.txt");
                        System.out.println("");
                    }
                    else
                    {
                        System.out.println("File Updated.");
                    }

            }while (!outputFile.exists()) ;

        try
            {

            //the "true" argument sets the FileWriter to append mode so that is does not overwrite the first line
                BufferedWriter out = new BufferedWriter(new FileWriter("[Insertion] Sorted Entries.txt", true));
                for (int i = 0 ; i < size ; i++)
                {
                    textContent = (insArray[i]) ;
                    out.write(textContent) ;
                    out.newLine() ;
                }

                out.close() ;
            }catch(IOException e)
            {
                System.out.println("Could not write to file") ;
                System.exit(0) ;
            }

         System.out.println("Time to execute: " + timer.getElapsedTime() + "ns"); 
    }

    private static void replace(Comparable[] array, int i, int j) 
    {
        Comparable swap = array[i];
        array[i] = array[j];
        array[j] = swap;
    }
}
问题回答

右眼......这样做会有所助益?

    for (int i = 1; i < size; i++)
        for (int j = i; j > 0; j--)
        {
            final int cmp = insArray[j-1].compareToIgnoreCase(insArray[j]);
            System.out.format("Comparing %s at %d to %s at %d, result %d
",
               insArray[j-1], j-1, insArray[j], j, cmp);
            if (cmp > 0) replace(insArray, j, j-1);
        }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签