2017-04-20 6 views
0

私の問題は、セレクションに間違ったスワップ数があることです。 常に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

コードを変更するだけで、それを削除し、 'Swapping' – 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

関連する問題