2017-04-20 55 views
0

我的問題是,selectionsort顯示錯誤的交換次數。 它總是顯示0或一些大數字。 當給數組排序,它總是表現出大量或全部其他未分類測試始終爲0使用排序數組進行選擇排序需要多少次交換?

//this class is called selectionSort. It sorts a given array. 
public class SelectionSort implements ISorter { 
    private int swaps; 

    public SelectionSort() {} 

    @Override 
    public ISortStats sort(int[] a) { 
     long time = System.nanoTime(); 
     int swapping = 0; 
     int numOfComparisons = 0; 
     for (int i = 0; i < a.length; i++) { 
      int min = i; 
      for (int j = i + 1; j < a.length; j++) { 
       numOfComparisons++; 
       if (a[min] >= a[j]) { 
        min = j; 
       } 
      } 
      swapping = swap(a, i, min, this.swaps); 
     } 
     long endTime = System.nanoTime(); 
     SortStats ss = new SortStats("Selection Sort", 
            a.length, 
            numOfComparisons, 
            swapping, 
            (endTime - time)); 
     return ss; 
    } 

    private int swap(int[] a, int i, int j, int swapping) { 
     int temp = a[i]; 
     a[i] = a[j]; 
     a[j] = temp; 
     return swapping++; 
    } 
} 
+2

這縮進......瘋了。 –

+0

不再:) :)。 –

+1

不完全確定,但這對我來說看起來很可疑:'swapping = swap(a,i,min,this.swaps);'。也許你的意思是'swap = swap(a,i,min,swapping);'? – Turing85

回答

0

我不知道爲什麼你有互換作爲一個類的成員,但此行絕對是錯誤的

swapping = swap(a, i, min, this.swaps); 

正如你永遠不會更新this.swaps

+0

我該如何改變它? @ControlAltDel – TeslaCarsForLife

+0

只需將其刪除並通過更改代碼來使用「交換」 – ControlAltDel

+0

@TeslaCarsForLife。我們無法告訴您應該如何編寫代碼。目前,您將'this.swaps'(如果未初始化並且未更改,將始終爲'0')的值傳遞給'swap(...)'。 – Turing85

0
public class SelectionSort implements ISorter { 
     private int swaps; 
     public SelectionSort(){ 
     } 

     @Override 
     public ISortStats sort(int[] a) { 
      long time = System.nanoTime(); 
      int swapping =0; 
      int numOfComparisons = 0; 
      for (int i=0; i<a.length; i++) { 
        int min = i;  

        for (int j=i+1; j < a.length; j++) { 
         numOfComparisons++; 
         if (a[min] >= a[j]) { 
           min = j; 

         } 

        } 

        swapping = swap(a, i, min, swapping); 

      } 

      long endTime = System.nanoTime(); 
      SortStats ss = new SortStats("Selection Sort", a.length, numOfComparisons, swapping, (endTime -time)); 
      return ss ; 
     }   

     private static int swap (int[] a, int i, int j, int swapping){ 
      if(!(order(a))){ 
        swapping++; 
        int temp = a[i]; 
        a[i] = a[j]; 
        a[j] = temp; 
      } 
      return swapping; 
     } 
     private static boolean order(int[] arr){ 
      int count = 0; 
      //this loop runs thru the array to check if it is in order. 
      for(int a = 0; a < arr.length-1; a++){ 
        if(arr[a] <= arr[a+1]){ // if true count plus 1 
         count++; 
        } 
      } 
      if(count == arr.length-1){ // checks if the count and arr length -1 is equal 
        return true; // if equal it will return true 
      } 
      return false; // returns false if the array is not correctly sorted. 
     } 


} 
+0

我解決了!謝謝你幫助我的人。 – TeslaCarsForLife