Search code examples
javaexecutionbubble-sort

BubbleSort Output Graph Trouble


Hi I have written a bubble sort application in JAVA. The application is working fully and give sufficient output, but it doesn't give me an out put for average time.

Here is my code:

public class BubbleSort {
static double bestTime = 10000000, worstTime = 0;
public static void main(String[] args) {
    int BubArray[] = new int[]{**#interger values are placed in here#**};
         System.out.println("Unsorted List Before Bubble Sort");
         for(int a = 0; a < BubArray.length; a++){
         System.out.print(BubArray[a] + " ");
            }

        System.out.println("\n Bubble Sort Execution ...");

                    for(int i=0; i<10000;i++) {
                        bubbleSortTimeTaken(BubArray, i);
                        }

         int itrs = bubbleSort(BubArray);
    System.out.println("");               
    System.out.println("Array After Bubble Sort");
    System.out.println("Moves Taken for Sort : " + itrs + " Moves.");
    System.out.println("BestTime: " + bestTime + " WorstTime: " + worstTime);
    System.out.print("Sorted Array: \n");

        for(int a = 0; a < BubArray.length; a++){
                System.out.print(BubArray[a] + " ");
        }
}

private static int bubbleSort(int[] BubArray) {

int z = BubArray.length;
int temp = 0;

int itrs = 0;

for(int a = 0; a < z; a++){
        for(int x=1; x < (z-a); x++){

                if(BubArray[x-1] > BubArray[x]){

                        temp = BubArray[x-1];
                        BubArray[x-1] = BubArray[x];
                        BubArray[x] = temp;


                }    

                itrs++;
        }
}

return itrs;
}

public static void bubbleSortTimeTaken(int[] BubArray, int n) 
{
long startTime = System.nanoTime();
    bubbleSort(BubArray);   
    double timeTaken = (System.nanoTime() - startTime);
    if (timeTaken > 0)
    {
        worstTime = timeTaken;
    }
    else if (timeTaken < bestTime)
    {
        bestTime = timeTaken;
    }

    System.out.println(n + "," + timeTaken);

}
}

But I am unsure of how to a) get the average time for each and also how I would plot the Best, Average and Worst Case results on a graph.

Here is a sample output:

Unsorted List Before Bubble Sort
#Integers Values of Unsorted List#

Bubble Sort Execution ... **#(execution number, time in nanoseconds)#**
0,6336584.0
1,5063668.0
2,3364580.0
3,3373289.0
4,3755912.0
5,3383866.0
....
9995,3431772.0
9996,3368312.0
9997,3743469.0
9998,4639362.0
9999,3433638.0

Moves Taken for Sort : 499500 Moves.
BestTime: 1.0E7 WorstTime: 3433638.0

Also I'm not sure if my bubbleSortTimeTaken() function works properly as 1.0E7 is given for every run of the program regardless of the number of integers used (100, 1000, 10000, 100000, 1000000) have been tested. I wish to find the average, best and worst case to plot.

Any help would be appreciated, thanks.


Solution

  •    if (timeTaken > 0)   // <- PROBLEM, Shouldn't be zero
        {
            worstTime = timeTaken;
        }
        else if (timeTaken < bestTime) //<- PROBLEM! , the 2 comparisons are unrelated
        {
            bestTime = timeTaken;
        }
    

    Right there. If timeTaken>0, which it will be mostly, the else if is never executed.
    Thus, bestTime never gets updated, and retains the original value (1E7).

    To fix this, the function should be modified to look like :

    public static void bubbleSortTimeTaken(int[] BubArray, int n) 
    {
    long startTime = System.nanoTime();
        bubbleSort(BubArray);   
        double timeTaken = (System.nanoTime() - startTime);
        if (timeTaken > worstTime)
        {
            worstTime = timeTaken;
        }
        if (timeTaken < bestTime)
        {
            bestTime = timeTaken;
        }
    
        System.out.println(n + "," + timeTaken);
    
    }
    }
    

    Note that I have added a possible fix about worstTime too.