2012-12-07 55 views
0

我已经写了一个BubbleSort程序,它工作的很好,给了我一个很好的输出并且充分地完成了它的工作。但是我无法通过一次排序重新执行程序。即该程序完成了10000个唯一的数字,并输出所需的时间和步骤的数量,但不会再执行,之后再说999次?JAVA编程执行多次

总之,任何人都可以帮助我的程序运行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 ... ] 
+1

什么是在一个新的方法提取'主()'代码和调用同1000倍的问题? –

回答

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


} 
} 
+0

有没有一种方法,而不是BubArray = new int {array ints}; ......再次调用BubArray(999次)并计算每次...所以1000个不同数量的“采取的时间”和“采取的动作”,这是否有意义? –

+0

BubArray的值每次进入循环时都会改变?如果是,那么怎么样?随机? – BLOB

+0

我的意思是每次执行都有相同的10000个唯一值,但我需要计算1000次执行的平均时间。 “您可以测量将数据分类1,000次需要多长时间,然后将此时间除以1,000以获得一次运行分类的平均值。” 。这是在图表上找出最差,平均,最佳案例。 –

-1

移动以下方法

private static int bubbleSort(int[] BubArray) 

以延伸Thread。也许你可以在每次执行完成时创建新的线程。根类中的静态实例变量可用于保存时间。

+0

线程似乎不是问题中的要求。无需增加不必要的复杂性。 –