Search code examples
javaruntimeexecutionbubble-sort

JAVA Programming Execute Multiple Times


I have written a BubbleSort program and it works great, gives me a good output and does its job sufficiently. But I am unable to make the program re-execute after sorting through once. I.e. the program completes a sort of 10000 unique numbers and outputs the time it takes and the amount of steps it took, but doesn't execute again, say for another 999 times after that?

In short, can anyone help me get my program to run through itself 1000 times so I am able to get an average of execution time?

Here is the code:

public class BubbleSort {
   public static void main(String[] args) {
      int BubArray[] = new int[] { #10000 unique values unsorted# };

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

      double timeTaken = bubbleSortTimeTaken(BubArray);
      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("Time Taken for Sort : " + timeTaken
            + " milliseconds.");
      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 double bubbleSortTimeTaken(int[] BubArray) {
      long startTime = System.nanoTime();
      bubbleSort(BubArray);
      long timeTaken = System.nanoTime() - startTime;
      return timeTaken;
   }
}

and here are the results output (note it is limited to just one run):

Unsorted List : 
[13981, 6793, 2662, 733, 2850, 9581, 7744 .... ]
Sorted List with BubbleSort

Moves Taken to Sort : 1447551 Moves.
Time Taken to Sort : 1.2483121E7 Milliseconds.

[10, 11, 17, 24, 35, 53, 57, 60, 78, 89, 92 ... ]

Solution

  • Edited code...

    public class BubbleSort {
        static double bestTime = 10000000, worstTime = 0;  //global variables
    public static void main(String[] args) {
        int BubArray[] = new int[]{3,5,3,2,5,7,2,5,8};
    
        System.out.println("Array Before Bubble Sort");
        for(int a = 0; a < BubArray.length; a++){
        System.out.print(BubArray[a] + " ");
    
        }
    
      System.out.println("\n Entering Loop...");
    
      for(int i=0; i<1000;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)/1000000d;
    
         if(timeTaken > 0)
         {
             if(timeTaken > worstTime)
             {
                 worstTime = timeTaken;
             }
             else if(timeTaken < bestTime)
             {
                 bestTime = timeTaken;
             }
    
         }
    
         System.out.println("Loop number: "+n + " Time Taken: " + timeTaken);
    
    
    }
    }