a.ymous
a.ymous

Reputation: 139

JAVA BubbleSort Run Time Trouble

I've written a bubble sort program that sorts 10000 unique values into order.

I've run the program and it gives me an output, but the output doesn't seem to look right to me.

Here is the code:

public class BubbleSort {
    public static void main(String[] args) {
        int BubArray[] = new int[]{#here are 10000 integers#};
        System.out.println("Array Before Bubble Sort");
            for(int a = 0; a < BubArray.length; a++){
                 System.out.print(BubArray[a] + " ");
            }

            double timeTaken = bubbleSortTimeTaken(BubArray);
                 bubbleSort(BubArray);
            System.out.println("");               
            System.out.println("Array After Bubble Sort");
            for(int a = 0; a < BubArray.length; a++){
        System.out.println("    Time taken for Sort : " + timeTaken + " milliseconds.");
                System.out.print(BubArray[a] + " ");
            }
    }

private static void bubbleSort(int[] BubArray) {

            int z = BubArray.length;
            int temp = 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;

                            }      
                    }
            }
    }
public static double bubbleSortTimeTaken(int[] BubArray) {
    long startTime = System.nanoTime();
        bubbleSort(BubArray);
    long timeTaken = System.nanoTime() - startTime;
    return timeTaken;
    }
}

The code runs smooth and no errors, but this is the output I receive:

Array Before Bubble Sort
#10000 integers randomly#
Array After Bubble Sort
Time taken for Sort : 1.0114869E7 milliseconds.
10  Time taken for Sort : 1.0114869E7 milliseconds.
11  Time taken for Sort : 1.0114869E7 milliseconds.
17  Time taken for Sort : 1.0114869E7 milliseconds.
24  Time taken for Sort : 1.0114869E7 milliseconds.
35  Time taken for Sort : 1.0114869E7 milliseconds.
53  Time taken for Sort : 1.0114869E7 milliseconds.

....

14940   Time taken for Sort : 1.0114869E7 milliseconds.
14952   Time taken for Sort : 1.0114869E7 milliseconds.
14957   Time taken for Sort : 1.0114869E7 milliseconds.
14958   Time taken for Sort : 1.0114869E7 milliseconds.
14994   Time taken for Sort : 1.0114869E7 milliseconds.
14997   Time taken for Sort : 1.0114869E7 milliseconds.
BUILD SUCCESSFUL (total time: 1 second)

The 1.0114869E7 milliseconds runs throughout the program, and I don't think the output is exactly what I'm trying to do, though it looks like. I wish to output time taken for the program to run through and also each sort time.

~I hope this makes sense. Any help would be appreciated, thanks.

Upvotes: 0

Views: 1151

Answers (1)

mtk
mtk

Reputation: 13717

I guess you might want to output this. The sysout should be before the for loop.

System.out.println(" Time taken for Sort : " + timeTaken + " milliseconds."); for(int a = 0; a < BubArray.length; a++){ System.out.print(BubArray[a] + " "); }

You have sorted down the array already, and you displaying it in the for loop later. The time you see output is the total time taken by bubble sort (approx.), which is being calculated in following method

public static double bubbleSortTimeTaken(int[] BubArray) {
    long startTime = System.nanoTime();
    bubbleSort(BubArray);
    long timeTaken = System.nanoTime() - startTime;
    return timeTaken;
    }
}

So, that's the total time.

Upvotes: 1

Related Questions