使用排序数组进行选择排序需要多少次交换?

时间:2017-04-20 19:07:24

标签: java

我的问题是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 :(得分:0)

我不知道为什么你有掉队作为班级成员,但这一行肯定是错误的

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

因为你永远不会更新this.swaps

答案 1 :(得分: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.
      }


}