-2

Currently trying to edit my old code from System.out.println() statements that display the results of a benchmark test to writing the results to 2 different .txt files. I am doing this so I can create a new program that uses JFileChooser to select one of the 2 .txt files and display the data on a JTable. The problem is the BufferedWriter is only writing the last set of data values 10x over so I cant display the correct results in my JTable.

BenchmarkSorts.java:

/**
 * 
 * BenchmarkSorts is going to generate arrays consisting of random integers the size of which is supplied from SortMain.java.
 * It will then send the randomly generated array to be sorted using the Quick Sort Algorithm both Iteratively and Recursively
 * a total of 50 times each and display the benchmark results to the user.
 */

//=========
//packages
//=========
import java.io.*;
import java.util.Random;

//==============================
//start of class BenchmarkSorts
//==============================
public class BenchmarkSorts {

    //==========
    //variables
    //==========
    private int[] unsortedArray;
    private int iterativeCount = 0;
    private int recursiveCount = 0;
    private long iterativeTime, recursiveTime;
    private int[] iterativeCountLog = new int[50];
    private int[] recursiveCountLog = new int[50];
    private int iCounter = 0;
    private int rCounter = 0;
    private long[] iterativeTimeLog = new long[50];
    private long[] recursiveTimeLog = new long[50];
    private int arraySize;
    
    private FileWriter iFileWriter = new FileWriter("iterativeResults.txt");
    private FileWriter rFileWriter = new FileWriter("recursiveResults.txt");
    
    private BufferedWriter iBuffer = new BufferedWriter(iFileWriter);
    private BufferedWriter rBuffer = new BufferedWriter(rFileWriter);

    //=========
    //invoking
    //=========
    private QuickSort quickSort = new QuickSort();

    //============
    //constructor
    //============
    public BenchmarkSorts(int[] sizes) throws Exception {

        //==========================================================
        //for loop to add the sizes of the arrays to be benchmarked
        //==========================================================
        for(int i = 0; i < sizes.length; i++){
            arraySize = sizes[i];
            @SuppressWarnings("unused")
            BenchmarkSorts bms = new BenchmarkSorts(arraySize);        
        }
    }

    //========================================================
    //method to add random numbers to the array index size,
    //then send them to be sorted iteratively and recursively
    //before being displayed with the benchmark results
    //========================================================
    private BenchmarkSorts(int n) throws Exception {

        //=================================
        //nested for loops to run 50 times
        //creating arrays
        //=================================
        for(int i = 0; i < 50; i++) {
            unsortedArray = new int[n];

            for(int j = 0; j < n; j++) {
                Random randNum = new Random();
                unsortedArray[j] = (randNum.nextInt(1000));
            }
            runSorts();
        }
        displayReport(n);
    }

    //=========================================================
    //method to sort unsortedArray iteratively and recursively
    //while keeping track of count, time(in nano seconds)
    //=========================================================
    public void runSorts() throws Exception {

        //=========================================
        //creating 2 temporary arrays to be sorted
        //=========================================
        int[] tempArray1 = unsortedArray;
        int[] tempArray2 = unsortedArray;

        //============================
        //iterative sort and trackers
        //============================
        quickSort.iterativeSort(tempArray1);
        int returnCount = quickSort.getCount();
        long returnTime = quickSort.getTime();
        iterativeCount = iterativeCount + returnCount;
        iterativeTime = iterativeTime + returnTime;
        iterativeCountLog[iCounter] = returnCount;
        iterativeTimeLog[iCounter] = returnTime;
        iCounter++;

        //============================
        //recursive sort and trackers
        //============================
        quickSort.recursiveSort(tempArray2);
        returnCount = quickSort.getCount();
        returnTime = quickSort.getTime();
        recursiveCount = recursiveCount + returnCount;
        recursiveTime = recursiveTime + returnTime;
        recursiveCountLog[rCounter] = recursiveCount;
        recursiveTimeLog[rCounter] = recursiveTime;
        rCounter++;
    }

    //===================================================
    //method to display averages and standard deviations
    //===================================================
    public void displayReport(int arraySize) throws IOException {   

        //==========
        //variables
        //==========
        double iterativeAverageCount = 0;
        double iterativeAverageTime = 0;
        double recursiveAverageCount = 0;
        double recursiveAverageTime = 0;
        double iterativeSDCount = 0;
        double iterativeSDTime = 0;
        double recursiveSDCount = 0;
        double recursiveSDTime = 0;

        //========================================================
        //averaging the trackers for both iterative and recursive
        //========================================================
        iterativeAverageCount = iterativeCount / 50;
        iterativeAverageTime = iterativeTime / 50;
        recursiveAverageCount = recursiveCount / 50;
        recursiveAverageTime = recursiveTime / 50;

        //==========================================
        //for loop to calculate standard deviations
        //==========================================
        for(int i = 0; i < 50; i++) {
            iterativeSDCount = iterativeSDCount + Math.pow((iterativeCountLog[i] - iterativeAverageCount), 2);
            iterativeSDTime = iterativeSDTime + Math.pow((iterativeTimeLog[i] - iterativeAverageTime), 2);
            recursiveSDCount = recursiveSDCount + Math.pow((recursiveCountLog[i] - recursiveAverageCount), 2);
            recursiveSDTime = recursiveSDTime + Math.pow((recursiveTimeLog[i] - recursiveAverageTime), 2);
        }

        //====================
        //standard deviations
        //====================
        iterativeSDCount = Math.pow(iterativeSDCount, .5) / arraySize;
        iterativeSDTime = Math.pow(iterativeSDTime, .5) / arraySize;
        recursiveSDCount = Math.pow(recursiveSDCount, .5) / arraySize;
        recursiveSDTime = Math.pow(recursiveSDTime, .5) / arraySize;

        //====================================================
        //for loops to BufferedWrite data to respective files
        //====================================================
        for (int i = 0; i < 10; i++)    {
            iBuffer.write(arraySize + " " + iterativeAverageCount +  " " + iterativeSDCount + " " + iterativeAverageTime + " " + iterativeSDTime);
            iBuffer.newLine();
        }
        for (int i = 0; i < 10; i++)    {
            rBuffer.write(arraySize + " " + recursiveAverageCount +  " " + recursiveSDCount + " " + recursiveAverageTime + " " + recursiveSDTime);
            rBuffer.newLine();
        }
        iBuffer.close();
        rBuffer.close();
        
        //==========================================
        //old output that I want to write to a file
        //==========================================
        System.out.println("Iterative Quick Sort Results: " + "\nData Set Size (n): " + arraySize +
                ", Average Critical Operation Count: " + iterativeAverageCount + ", Standard Deviation of Count: " +
                iterativeSDCount + ", Average Execution Time: " + iterativeAverageTime + ", Standard Deviation of Time: " +
                iterativeSDTime);

        System.out.println("Recursive Quick Sort Results: " + "\nData Set Size (n): " + arraySize +
                ", Average Critical Operation Count: " + recursiveAverageCount + ", Standard Deviation of Count: " +
                recursiveSDCount + ", Average Execution Time: " + recursiveAverageTime + ", Standard Deviation of Time: " +
                recursiveSDTime);
    }
}

This is the old output using System.out.println() statements:

Iterative Quick Sort Results: 
Data Set Size (n): 100, Average Critical Operation Count: 66.0, Standard Deviation of Count: 0.13490737563232041, Average Execution Time: 11528.0, Standard Deviation of Time: 394.80891580611495
Recursive Quick Sort Results: 
Data Set Size (n): 100, Average Critical Operation Count: 99.0, Standard Deviation of Count: 199.0490957025427, Average Execution Time: 36124.0, Standard Deviation of Time: 78349.10253729777
Iterative Quick Sort Results: 
Data Set Size (n): 200, Average Critical Operation Count: 133.0, Standard Deviation of Count: 0.11224972160321825, Average Execution Time: 27364.0, Standard Deviation of Time: 544.5295033329232
Recursive Quick Sort Results: 
Data Set Size (n): 200, Average Critical Operation Count: 199.0, Standard Deviation of Count: 200.05439416568686, Average Execution Time: 29262.0, Standard Deviation of Time: 27784.950723818103
...
...
Iterative Quick Sort Results: 
Data Set Size (n): 12000, Average Critical Operation Count: 11000.0, Standard Deviation of Count: 2.6352313834736497E-4, Average Execution Time: 720028.0, Standard Deviation of Time: 69.97877063001957
Recursive Quick Sort Results: 
Data Set Size (n): 12000, Average Critical Operation Count: 11999.0, Standard Deviation of Count: 201.04293765444527, Average Execution Time: 6.034408E7, Standard Deviation of Time: 1011471.3254720564

As you can see it produces the correct outputs for all arraySize's from [100...12000] (I cut out the mid section to shorten the total length).

The 2 .txt files produced however only display the last arraySize [12000] 10x and I can't figure out why

iterativeResult.txt:

12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957
12000 11000.0 2.6352313834736497E-4 720028.0 69.97877063001957

recursiveResult.txt:

12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
12000 11999.0 201.04293765444527 6.034408E7 1011471.3254720564
  • `write(arraySize + " " + iterativeAverageCount + " " + iterativeSDCount + " " + iterativeAverageTime + " " + iterativeSDTime);`: it is writing exactly what you told it to write here. If that's not what you want, fix it. Unclear what you're asking. – user207421 Mar 03 '22 at 01:17
  • 1
    It seems wrong to close the files `iBuffer.close(); rBuffer.close();` immediately after writing the same result 10 times. It also looks like the `unsortedArray` should be cloned instead of assigned. – gwell Mar 03 '22 at 01:21
  • You are looping for your file output, but not for your sysout output - why? Also as @gwell says *It seems wrong to close the files iBuffer.close(); rBuffer.close();* - You also should not be creating multiple instances of this class as each one will have their own variables. – Scary Wombat Mar 03 '22 at 01:59
  • Too much code. A [mcve] would be helpful. – Robert Mar 03 '22 at 02:53

1 Answers1

-1

It looks generally confused.

//========================================================
//method to add random numbers to the array index size,
//then send them to be sorted iteratively and recursively
//before being displayed with the benchmark results
//========================================================
private BenchmarkSorts(int n)  

That's a constructor, not a method, so each use of it will construct (!) a new BenchmarkSorts instance, each of which has its own set of instance variables. And you apparently know it's a constructor, because to use it you wrote new BenchmarkSorts(...) - which means that creating one object using the first constructor ends up creating many of them using the second constructor, none of which instances are really used. Probably not a good idea in retrospect to have 'fixed' the unused-variable warning by suppressing it.

And this is probably the source of your bug. Each instance creates new BufferedWriters, each of which overwrites the same file.

Also,

int[] tempArray1 = unsortedArray;
int[] tempArray2 = unsortedArray;

You still have exactly one array.

Your first fix should be to turn the 2nd constructor into a method. Then worry about whether you really wanted 2 copies of 1 array.

passer-by
  • 1,103
  • 2
  • 4