首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用已排序数组进行选择排序需要交换多少次?

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

Stack Overflow用户
提问于 2017-04-21 03:07:24
回答 2查看 235关注 0票数 0

我的问题是,selectionsort显示了错误的交换数量。它总是显示0或某个较大的数字。当给定的数组被排序时,它总是显示一个很大的数字,或者所有其他未排序的测试总是0。

代码语言:javascript
复制
//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++;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-04-21 03:14:50

我不知道你为什么把swaps作为一个类的成员,但是这行绝对是错的

代码语言:javascript
复制
swapping = swap(a, i, min, this.swaps);

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

票数 0
EN

Stack Overflow用户

发布于 2017-04-21 04:09:22

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43527914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档