0

我编写了一个 BubbleSort 程序,它运行良好,给了我一个很好的输出并且充分地完成了它的工作。但是我无法在整理一次后重新执行程序。即程序完成了一种 10000 个唯一数字并输出它所花费的时间和所花费的步数,但不会再次执行,比如在那之后再执行 999 次?

简而言之,任何人都可以帮助我让我的程序运行 1000 次,以便我能够获得平均执行时间吗?

这是代码:

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;
   }
}

以下是结果输出(注意它仅限于一次运行):

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 ... ]
4

2 回答 2

1

修改后的代码...

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);


}
}
于 2012-12-07T08:29:14.670 回答
-1

移动下面的方法

private static int bubbleSort(int[] BubArray) 

在其他一些类扩展Thread中。可能您可以在每次执行完成时创建新线程。根类中的静态实例变量可用于保存时间。

于 2012-12-07T08:28:41.177 回答