2012-12-06 63 views
1

我寫了一個冒泡排序程序,它將10000個唯一值按順序排序。Java Bubble Sort Trouble

我已經運行了程序,它給了我一個輸出時間(使用nanoTime)來完成程序所需的時間,但是我希望將另一個輸出添加到程序的代碼中;程序從開始到結束進行排序需要多少次移動。

下面是代碼:

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"); 
    System.out.println(" Time taken for Sort : " + timeTaken + " milliseconds.");     
      for(int a = 0; a < BubArray.length; a++){ 
        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; 
} 
} 

的代碼執行和輸出,我如何想:

Array Before Bubble Sort 
13981 6793 2662 10986 733 10107 2850 ... 
Array After Bubble Sort 
10 11 17 24 35 53 57 60 61 78 83 89 128 131 138 141 .... 
Time taken for Sort : 1.6788472E7 milliseconds. 

但我想另一個輸出添加到它告訴我如何代碼許多動作(基本上是一個移動計數器)它需要完成,即:

Time taken for Sort : 1.6788472E7 milliseconds. 
Total number of moves: 3000 

這是否有意義? 任何幫助,將不勝感激,謝謝。

+0

有意義的是什麼?你爲什麼對這樣的價值感興趣? – SJuan76

+0

聽起來就像你需要聲明一個變量來跟蹤移動次數,並且每當算法移動一個值時就爲其添加一個(++?)。然後你只需在最後輸出該變量。 – jball

+0

順便說一句,我們你叫'bubbleSort(BubArray);'兩次?一次在'bubbleSortTimeTaken'中,一次在'main'中?如果您想獲得跑步時間,只需從'main'中刪除一個呼叫 – bhuang3

回答

1

我會改變bubbleSort返回一個int並分配time complexity它。

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; 

}

然後在main

int itrs = bubbleSort(BubArray); 
      System.out.println("");    
      System.out.println("Array After Bubble Sort"); 
    System.out.println(" Time taken for Sort : " + timeTaken + " milliseconds."); 
    System.out.println("Time complexity: " + itrs); 
+0

「itrs」我猜是你用來整數的名字? –

+0

我剛剛清楚,「時間複雜度」是每次完成的移動次數?如:排序時間:3.8208782E7毫秒。 時間複雜度:1447551 –

+0

是的,@JohnSmith – amphibient

1

試試這個:

// returns the number of switches 
private int bubbleSort(int[] BubArray) { 

     int z = BubArray.length; 
     int temp = 0; 
     int timesSwitched = 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; 
           timesSwitched++ 
         }  
       } 
     } 
    return timesSwitched; 
} 
+0

感謝您的意見,我感謝您的幫助和迅速回復。雖然foampile的回答真的把我的問題整理出來了。 –