【问题标题】:java - Selection Sortjava - 选择排序
【发布时间】:2021-11-20 15:34:46
【问题描述】:

我想实现一个选择对齐,接收 10 个整数并按升序组织它们。

但是,当我的代码运行时,其他的一切正常,只是第一个整数没有对齐。

请告诉我如何修复代码。

public static void sort(int[] array) {
    Scanner sc = new Scanner(System.in);
    System.out.println("put the int");
    
    for (int i =0;i <array.length;i++) {
        System.out.print((i+1)+": ");
    int n = sc.nextInt();
        array[i] = n;
        for (int j = 1; j < array.length;j++) {
            if (array[i] < array[j]) {
                
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
    }
    
    for (int a=0; a< array.length; a++) {
        System.out.print(array[a]+" ");

    }
}
public static void main(String[] args) {
    int[] my_array = {0,0,0,0,0,0,0,0,0,0};
    sort(my_array);
}

}

【问题讨论】:

  • 我知道这“只是”一个编码练习,但是让排序功能创建一个扫描仪并从终端获取输入只是一个非常糟糕的设计。

标签: java eclipse selection-sort


【解决方案1】:

你应该设置

int j = 0

在里面

【讨论】:

    【解决方案2】:

    如果你想实现Selctionsort

    • int j = i+1初始化内部for
    • 如果 array[i] 大于 array[j] 则更改数字,而不是相反
            for (int i =0;i <array.length;i++) {
                int minValue = array[i];
    
                for (int j = i +1; j < array.length;j++) {
                    if (array[i] > array[j]) {
                        
                        int temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
            
            for (int a=0; a< array.length; a++) {
                System.out.print(array[a]+" ");
            }
    

    【讨论】:

      【解决方案3】:

      您需要先读取整个数组,然后才能对其进行排序。另外,我不认为您的算法是真正的选择排序。在选择排序中,您必须在未排序的数据数组中找到最小值。 然后你交换。您的算法并没有完全做到这一点。

      为了说明,我将代码分解为函数。

      // Find the minimum value in the array, starting the search at "start"
      // Returns the index of the minimum
      static int findMinIndex(int[] array, int start)
      {
          int min = array[start];
          int minIndex = start;
          for (int i = start + 1; i < array.length; i++) {
              if (array[i] < min)  {
                  min = array[i];
                  minIndex = i;
              }
          }
          return minIndex;
      }
      
      // Swap 2 elements of an array
      static void swap(int[] array, int index1, int index2)
      {
          int temp = array[index1];
          array[index1] = array[index2];
          array[index2] = temp;
      }
      
      // Selection sort the array, ascending
      static void selectionSort(int[] array)
      {
          for (int i = 0; i < array.length; i++) {
              // First find the minimum from i to the end of the array...
              int minIndex = findMinIndex(array, i);
              // ...then swap
              if (minIndex != i) {
                  swap(array, i, minIndex);
              }
          }
      }
      

      【讨论】:

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