2012-10-05 65 views
0

所以這段代碼沒有計算比較數,但不計算掉(每個循環中的計數交換)。爲什麼我的比較計數功能不計算掉期?

有誰知道爲什麼,是不是太「嵌入或東西感謝所有幫助一百萬

*/ 
package sorts; 

import java.util.*; 

public class Sorts { 

    Random rand = new Random(); 

    private int countcomp; 
    private int countswap; 

    public Sorts() { 
     countcomp = 0; 
     countswap = 0; 
    } 

    public int getcomparisions() { 
     return countcomp; 
    } 

    public int getswaps() { 
     return countswap; 
    } 

    public static void main(String args[]) { 

     Sorts sorts = new Sorts(); 

     //int[] unsorted = {2, 4, 1, 9, 5, 10, 3, 6, 8, 7}; 
     //int[] unsorted = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; 
     //int[] unsorted = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
     int[] unsorted = new int[100]; 
     for(int i = 0; i < 100; i++){ 
      unsorted[i] = i; 
    } 
     //GENERATOR OF INT ARRAYS 

     System.out.println("\n\tSelection sort"); 
     Sorts sortsselect = new Sorts(); 
     sortsselect.selection(unsorted); 
     System.out.println("\nSwap Count : " + sortsselect.getswaps() + "\nComparision Count : " + sortsselect.getcomparisions()); 

     System.out.println("\n\tBubble sort"); 
     Sorts sortsbubble = new Sorts(); 
     sortsbubble.bubble(unsorted); 
     System.out.println("\nSwap Count : " + sortsbubble.getswaps() + "\nComparision Count : " + sortsbubble.getcomparisions()); 

     System.out.println("\n\n\tInsertion sort"); 
     Sorts sortsinsertion = new Sorts(); 
     sortsinsertion.insertion(unsorted); 
     System.out.println("\nSwap Count : " + sortsinsertion.getswaps() + "\nComparision Count : " + sortsinsertion.getcomparisions()); 

     System.out.println("\n\n\tExchange sort"); 
     Sorts sortsexchange = new Sorts(); 
     sortsexchange.exchange(unsorted); 
     System.out.println("\nSwap Count : " + sortsexchange.getswaps() + "\nComparision Count : " + sortsexchange.getcomparisions()); 

    } 

    //selection takes in a unsorted array and returns a sorted one 
    public void selection(int[] selunsorted) { 

     int i, j, max; 
     countcomp = 0; 
     countswap = 0; 
     max = selunsorted.length; 

     //iterate through the array and move the smallest number into an incrementing position 
     for (i = 0; i < max - 1; i++) { 
      int smallpos = i; 
      int smallest = selunsorted[i]; 

      for (j = i + 1; j < max; j++) { 
       countcomp++; 
       if (selunsorted[j] < smallest) { 
        countswap++; 
        smallest = selunsorted[j]; 
        smallpos = j; 
       } 
      } 
      int temp = selunsorted[i]; 
      selunsorted[i] = selunsorted[smallpos]; 
      selunsorted[smallpos] = temp; 
     } 
     for (i = 0; i < max; i++) { 
      j = selunsorted[i]; 
      System.out.print(j + ","); 
     } 
    } 

    public void bubble(int[] bubunsorted) { 

     int max = bubunsorted.length; 
     int i, imax, j; 
     countcomp = 0; 
     countswap = 0; 

     for (imax = max; imax > 0; imax--) { 
      for (j = 0; j + 1 < imax; j++) { 
       countcomp++; 
       if (bubunsorted[j] > bubunsorted[j + 1]) { 
        int temp = bubunsorted[j + 1]; 
        bubunsorted[j + 1] = bubunsorted[j]; 
        bubunsorted[j] = temp; 
        countswap++; 
       } 
      } 
     } 
     for (i = 0; i < max; i++) { 
      j = bubunsorted[i]; 
      System.out.print(j + ","); 
     } 
    } 

    public void insertion(int[] insertunsorted) { 

     int i, j, a; 
     int max = insertunsorted.length; 
     countcomp = 0; 
     countswap = 0; 

     for (i = 0; i < max; i++) { 
      for (j = 0; j < i; j++) { 
       countcomp++; 
      } 
      if (insertunsorted[j] > insertunsorted[i]) { 
       int temp = insertunsorted[j + 1]; 
       insertunsorted[j + 1] = insertunsorted[i]; 
       countswap++; 
       for (a = (j + 2); a < (i + 1); a++) { 
        int temp1 = insertunsorted[a]; 
        insertunsorted[a] = temp; 
        temp = temp1; 
        countswap++; 
       } 
      } 
     } 

     for (i = 0; i < max; i++) { 
      j = insertunsorted[i]; 
      System.out.print(j + ","); 
     } 
    } 

    public void exchange(int[] exchangeunsorted) { 

     int max = exchangeunsorted.length; 
     int i, j; 
     countcomp = 0; 
     countswap = 0; 

     for (i = 0; i < max; i++) { 
      for (j = i; j < max; j++) { 
       countcomp++; 
       if (exchangeunsorted[j] < exchangeunsorted[i]) { 
        countswap++; 
        int temp = exchangeunsorted[j]; 
        exchangeunsorted[i] = exchangeunsorted[j]; 
        exchangeunsorted[j] = temp; 
       } 
      } 
     } 
     for (i = 0; i < max; i++) { 
      j = exchangeunsorted[i]; 
      System.out.print(j + ","); 
     } 
    } 
} 
+0

您是否嘗試過使用'System.out.println',它們在確定程序流程中非常有用,並檢查它出錯的地方 – Abubakkar

回答

0

請參閱數組初始化開始你的main()方法:? -

int[] unsorted = new int[100]; 
for(int i = 0; i < 100; i++){ 
     unsorted[i] = i; 
} 

你的數組已經排序。所以,不會發生互換髮生。所以,swapCount是0 ..不過還是比較會做..因此,他們都算..

嘗試用更小尺寸的陣列。而添加隨機值吧..這是沒有排序。它會運行

0

呀,你要創建一個有序數組,所以沒有掉期正在發生。 你應該做這樣的事情:

Random r = new Random(); 
for(int i = 0; i < 100; i++) { 
unsorted[i] = r.nextInt(500); 
} 

您還需要爲每個不同的排序算法,生成新排序的數組(或直接複製使用Arrays.copyOf(陣列)的陣列),作爲數組進行排序通過第一排序算法。如果你這樣做,交換顯示正確。

此外,我複製並測試了您的代碼,並且在交換排序或插入排序的某處出現錯誤,因爲交換排序的輸出大多爲1。

相關問題