【问题标题】:Java Selection SortJava 选择排序
【发布时间】:2017-04-25 01:12:29
【问题描述】:

我在让我的排序检查每个索引时遇到问题。它跳过了j 的第三个索引,因为它转到i[0]j[2]i[0]j[4] 我不知道它为什么这样做?另外,我的号码实际上被交换时遇到了麻烦。有人知道我的错误在哪里吗?

static void selectionSort(int[] arr) {
    final long startTime = System.nanoTime(); // starts timer
    System.out.println("Selection Sort");
    //************** Code For Sorting *****************//
    //*************************************************//
    int counter = 0;
    int first = 0;
    int second = 0;
    
    // Copies unsorted array to new array
    //int[] sorted = Arrays.copyOf(arr, arr.length);
    
    // sorts unsorted array for comparison later on
    //Arrays.sort(sorted);
    
    // comparing the first index value to
    // the rest of the values in the array
    for (int i = 0; i < arr.length - 1; i++) {
        int minIndex = i;

        // representing the next index value 
        // to the original and comparing it
        for (int j = 1; j < arr.length - 1; j++) {
            int nextIndex = j;

            if (arr[minIndex] < arr[nextIndex]) {
                System.out.println("i: " + i);
                System.out.println("j: " + j);
                System.out.println("Checking next index");
            }
            if (arr[minIndex] > arr[nextIndex]) {
                System.out.println("i: " + i);
                System.out.println("j: " + j);
                //counter = j; // getting array index
                first = arr[minIndex];
                second = arr[nextIndex];
                minIndex = second;
                arr[minIndex] = second;
                System.out.println("first:" + first);
                System.out.println("second:" + second);
                System.out.println("minIndex:" + minIndex);
                System.out.println("arr[minIndex]:" + arr[minIndex]);
                System.out.println("Possible lowest unsorted value");
            }
            //Swap here
            if (arr[arr.length - 1] == arr[j]) {
                arr[0] = second;
                arr[counter] = first;
                counter = 0;
                //minIndex= i+1;
            }
        }
        for (int k = 0; k < arr.length; k++) {
            System.out.print(arr[k] + ", ");
        }
        System.out.println();
    }
}

【问题讨论】:

    标签: java arrays sorting swap selection-sort


    【解决方案1】:

    您犯的第一个错误是在嵌套的for 循环中。对于外部 for 循环的每次迭代,内部循环的起始索引 (j) 应始终从 i + 1 开始(索引器 i 前面的一个位置)not @987654326 @正如你所做的那样。

    其次,通过使用条件j &lt; arr.length-1,您将始终排除数组中的最后一个元素。

    改变这个:

    for(int j = 1; j < arr.length-1; j++)
    

    到这里:

    for(int j = i + 1; j < arr.length; j++)
    

    继续,您的算法似乎存在一些问题,包括您的 swap 功能,所以,让我们重新开始。

    选择排序是一种基于就地比较的算法,其中数组分为两部分,左端的排序部分和右端的未排序部分。最初,已排序部分为空,未排序部分为整个数组。

    从未排序的数组中选择最小的元素并与最左边的元素交换,该元素成为已排序数组的一部分。此过程继续将未排序的数组边界向右移动一个元素。

    考虑到这一点,现在我们可以开始算法了。

    public static void selectionSort(int[] arr){
        for(int i = 0; i < arr.length-1; i++){
            int minIndex = i;  // smallest element index
            for(int j = i + 1; j < arr.length; j++){
                if(arr[j] < arr[i]){  // find smallest element
                    if(arr[j] < arr[minIndex])
                    minIndex = j; // update smallest element index
                }
            }
    
            if(i != minIndex){  // swap
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
        // print the result
        Arrays.stream(arr).forEach(System.out::println); 
    }
    

    附带说明,选择排序复杂度为Ο(N2),其中N 是数组中的元素数。

    【讨论】:

      【解决方案2】:

      选择排序的算法如下:

      public static void selectionSort(int[] arr) {
          // iterate over all subsets of the array
          // (0-last, 1-last, 2-last, 3-last, ...)
          for (int i = 0; i < arr.length; i++) {
              // assume the min is
              // the first element
              int min = arr[i];
              // index of the
              // min element
              int min_i = i;
              // check the elements
              // after i to find
              // the smallest
              for (int j = i + 1; j < arr.length; j++) {
                  // if this element
                  // is less, then it
                  // is the new min
                  if (arr[j] < min) {
                      min = arr[j];
                      min_i = j;
                  }
              }
              // if min element is not
              // equal to the current
              // one, then swap them
              if (i != min_i) {
                  int temp = arr[i];
                  arr[i] = arr[min_i];
                  arr[min_i] = temp;
              }
          }
      }
      
      public static void main(String[] args) {
          int[] arr = {5, 34, 1, 15, 3, 8, 9, 2, 7, 6, 43, 4};
          selectionSort(arr);
          System.out.println(Arrays.toString(arr));
          // [1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 34, 43]
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多