Results 1 to 3 of 3
  1. #1
    Vysero is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Having trouble working with files

    Here is the program:

    Java Code:
    package sortitout;
    
    import javax.swing.JOptionPane;
    
    import java.io.FileWriter;
    
    import java.io.IOException;
    
    import java.io.PrintWriter;
    
    import java.util.Random;
    
    // Method main in this class provides a template for testing sorting (and other) 
    // algorithms for their running times. The computed running times are printed as a 
    // a comma separated value text file that can be opened in a spreadsheet program
    // in which one can automatically generate graphs
    public class SortTester {
    
        public static void main(String args[]) {
    
            int numberOfSizes = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of input sizes to be tested"));
    
            int startingSize = Integer.parseInt(JOptionPane.showInputDialog("Enter the starting input size"));
    
            int sizeIncrement = Integer.parseInt(JOptionPane.showInputDialog("Enter the input size increment"));
    
            int runsPerSize = Integer.parseInt(JOptionPane.showInputDialog("Enter the runs per list size"));
    
            int seed = Integer.parseInt(JOptionPane.showInputDialog("Enter a random number generator seed"));
    
            int listSize = (numberOfSizes * sizeIncrement);
    
            Integer[] randomList = new Integer[listSize];
    
            Random randomIntegerGenerator;
    
            long startTime, endTime;
    
            long[] timingsPerSizeQuicksort = new long[numberOfSizes];
    
            long[] timingsPerSizeSelectionSort = new long[numberOfSizes];
    
            try {
    
     // Open the file sortTestResults.csv in folder Files, which you
     // must create in in the top level of this project directory
                PrintWriter resultsFile
                        = new PrintWriter(new FileWriter("Files/sortTestResults.csv"));
    
     // initialize the timing results array for each sorting routine 
     // for each size to 0
                for (int size = 0; size < numberOfSizes; size++) {
    
                    timingsPerSizeQuicksort[size] = 0;
    
                    timingsPerSizeSelectionSort[size] = 0;
    
                }
    
     // At each size, run each sorting routine runsPerSizeTime times
                randomIntegerGenerator = new Random(seed);
    
                for (int run = 1; run <= runsPerSize; run++) {
    
     // get a new seed from the random number generator to generate the
     // random lists for this run on this size of list
                    seed = randomIntegerGenerator.nextInt();
    
                    int currentListLength = sizeIncrement;
    
                    int sizeNumber = 0;
    
     // run each sorting routine on the same random array of each size 
     // for all sizes, starting with sizeIncrement and increasing by 
     // sizeIncrement on each pass through the loop
                    while (currentListLength <= listSize) {
    
     // create a randomly generated list of the current size 
                        randomIntegerGenerator = new Random(seed);
    
                        for (int i = 0; i < currentListLength; i++) {
    
                            randomList[i] = randomIntegerGenerator.nextInt();
    
                        }
    
     // run quicksort on oon this random list, recording its time
                        startTime = System.nanoTime();
    
                        QuickSort.quickSort(randomList, 0, currentListLength - 1);
    
                        endTime = System.nanoTime();
    
                        timingsPerSizeQuicksort[sizeNumber]
                                = timingsPerSizeQuicksort[sizeNumber] + endTime - startTime;
    
     // check Quicksort for accuracy
    // System.out.println("Quicksort");
    // int i=0; 
    // while (i < currentListLength-1 && (randomList[i] <= randomList[i+1])){
    // i++;
    // }
    // if (i == currentListLength-1){
    // System.out.println("List was sorted properly");
    // }
    // else {
    // System.out.println("List was not sorted properly: "
    // + "List[" + (i) + "] = " + randomList[i]
    // + " and List[" + (i+1) + "] = " + randomList[i+1] + ".");
    // }
     // create the same randomly generated list of the current size
     // by using the same seed as above.
                        randomIntegerGenerator = new Random(seed);
    
                        for (int k = 0; k < currentListLength; k++) {
    
                            randomList[k] = randomIntegerGenerator.nextInt();
    
                        }
    
     // run SelectionSort on this same array, recording its time
                        startTime = System.nanoTime();
    
                        SelectionSort.selectionSort(randomList, 0, currentListLength - 1);
    
                        endTime = System.nanoTime();
    
                        timingsPerSizeSelectionSort[sizeNumber]
                                = timingsPerSizeSelectionSort[sizeNumber] + endTime - startTime;
    
    // //Check Selection Sort for accuracy
    // System.out.println("Selection Sort");
    // i=0; 
    // while (i < currentListLength-1 && (randomList[i] <= randomList[i+1])){
    // i++;
    // }
    // if (i == currentListLength-1){
    // System.out.println("List was sorted properly");
    // }
    // else {
    // System.out.println("List was not sorted properly: "
    // + "List[" + (i) + "] = " + randomList[i]
    // + " and List[" + (i+1) + "] = " + randomList[i+1] + ".");
    // }
     // increment the size number and the actual size of currentLength
                        
                        
                        
                        sizeNumber++;
    
                        currentListLength = currentListLength + sizeIncrement;
    
                    }
    
                }
    
     // The sums of the timings for each run per size for each sort for each
     // problem size are the respective timingsPerSize arrays, so average these
     // times and print them to the csv file, one line per sort, labeled with
     // the name of the sort type as the first element.
                resultsFile.print("Quicksort");
    
                for (int i = 0; i < numberOfSizes; i++) {
    
                    resultsFile.print(", " + (float) timingsPerSizeQuicksort[i] / (float) runsPerSize / 1000000.0);
    
                }
    
                resultsFile.println();
    
                resultsFile.print("SelectionSort");
    
                for (int i = 0; i < numberOfSizes; i++) {
    
                    resultsFile.print(", " + (float) timingsPerSizeSelectionSort[i] / (float) runsPerSize / 1000000.0);
    
                }
    
                resultsFile.println();
    
                resultsFile.close();
    
            } catch (IOException ex) {
    
                ex.printStackTrace(System.err);
    
                System.exit(1);
    
            }
    
        }
    
    }
    Java Code:
    package sortitout;
    
    public class SelectionSort {
    
        public static <T extends Comparable<T>>
                void selectionSort(T[] table, Integer low, Integer high) {
    
            int n = high;
    
    // System.out.println(n);
            for (int fill = low; fill < n; fill++) {
    
                int posMin = fill;
    
                int next = fill + 1;
    
                while (next <= n) {
    
    // System.out.println("here " + n + " " +fill + " " + next);
                    if (table[next].compareTo(table[posMin]) < 0) {
    
                        next++;
    
                    }
    
                    T temp = table[fill];
    
                    table[fill] = table[posMin];
    
                    table[posMin] = temp;
    
                }
    
            }
        }
    }
    Java Code:
    package sortitout;
    
    public class QuickSort {
    
        public static <T extends Comparable<T>> void quickSort(T[] table, int first, int last) {
    
            if (first < last) {
    
                int pivIndex = partition(table, first, last);
    
                quickSort(table, first, pivIndex - 1);
    
                quickSort(table, pivIndex + 1, last);
    
            }
    
        }
    
        protected static <T extends Comparable<T>> int partition(T[] table, int first, int last) {
    
            T pivot = table[first];
    
            int up = first;
    
            int down = last;
    
            do {
    
                while ((up < last) && (pivot.compareTo(table[up]) >= 0)) {
    
                    up++;
    
                }
    
                while (pivot.compareTo(table[down]) < 0) {
    
                    down--;
    
                }
    
                if (up < down) {
    
                    swap(table, up, down);
    
                }
    
            } while (up < down);
    
            swap(table, first, down);
    
            return down;
    
        }
    
        private static <T extends Comparable<T>> void swap(T[] table, int first, int last) {
    
            T temp = table[first];
    
            table[first] = table[last];
    
            table[last] = temp;
    
        }
    
    }
    So the problem is the program does not stop running. I didn't actually create the program myself. Our teacher gave it to us. I have already emailed him but he is currently not responding. So I came here in hopes some one could help. I have tried opening the file in Excel while it is still running and it gives me guff about the program being used. Like I said it does not stop running and there does not seem to be a confirmation of any kind written into the program.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Having trouble working with files

    Debug it.
    Place a load of System.out.println() calls in there printing out various variables to see what values it thinks it's working with at different points of the run and that might help you to narrow it down.
    As it stands that's far too much code to trawl through on a forum.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Vysero is offline Member
    Join Date
    Feb 2012
    Posts
    6
    Rep Power
    0

    Default Re: Having trouble working with files

    Okay I did some experimenting I think this program just takes a LONG long long time to run lol cause it works for values 1,1,1,1,1 or 2,2,2,2,2 it just takes a bit so the values I am suppose to enter are much larger

Similar Threads

  1. Trouble effectively working with arrays
    By javaStooge in forum New To Java
    Replies: 9
    Last Post: 02-09-2014, 07:51 PM
  2. Trouble with JAR files
    By MiniatureBeast in forum Eclipse
    Replies: 3
    Last Post: 07-17-2011, 10:45 PM
  3. Trouble compiling java files
    By winterkid09 in forum Java Applets
    Replies: 2
    Last Post: 11-21-2010, 11:25 AM
  4. Trouble working with/calling Objects! Any Help?
    By ramathews in forum New To Java
    Replies: 4
    Last Post: 03-24-2010, 03:50 PM
  5. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 12:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •