【问题标题】:Error in Java Insertion SortJava插入排序中的错误
【发布时间】:2018-07-01 11:26:24
【问题描述】:

我尝试通过书中的伪代码在 java 中编写插入排序。此代码的输出应该是按升序排列的数字,但由于某种原因,我得到了 10、4、5、6、7、8、9。任何帮助是极大的赞赏!谢谢!

public class InsertionSort {

    public int Array[] = {10,9,8,7,6,5,4};

    public static void main(String[] args) {
        InsertionSort obj1 = new InsertionSort();
    }

    public InsertionSort() {
        InsertionSortMethod();
        PrintArray();
    }

    public void InsertionSortMethod() {
        for(int j = 2; j < Array.length; j++) {
            int key = Array[j];
            int i = j - 1;
            while(i > 0 && Array[i] > key) {
                Array[i + 1] = Array[i];
                i = i - 1;
            }
            Array[i + 1] = key;
        }   
    }

    public void PrintArray() {
        for(int i = 0; i < Array.length; i++) {
            System.out.println(Array[i]);
        }
    }
}

【问题讨论】:

    标签: java arrays sorting insertion-sort


    【解决方案1】:

    像这样从 j=1 开始 for 循环:

    for(int j = 1; j < array.length; j++) {
    

    并像这样修改while循环条件:

    while(i >= 0 && array[i] > key) {
    

    正确的工作代码:

    public class InsertionSort {
    
    public int array[] = {10,9,8,7,6,5,4};
    
    public static void main(String[] args) {
        InsertionSort obj = new InsertionSort();
        obj.insertionSortMethod();
        obj.printArray();
    }
    
    
    public void insertionSortMethod() {
        for(int j = 1; j < array.length; j++) {
            int key = array[j];
            int i = j - 1;
            while(i >= 0 && array[i] > key) {
                array[i + 1] = array[i];
                i = i - 1;
        }
            array[i + 1] = key;
    
        }   
    }
    
    public void printArray() {
        for(int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
    
    }
    

    【讨论】:

      【解决方案2】:

      在代码的第三行,即

      InsertionSort obj1 = new InsertionSort();
      

      您正在创建一个 InsertionSort 类的对象,但在您的代码中,它被定义为一个函数,我认为它是一个构造函数,您必须提及该类以方便读者。

      除此之外,您还以 2 for(int j = 2; j &lt; Array.length; j++) 开始循环 为什么这样?你的一个元素错过了,所以从 1 开始 j :)

      【讨论】:

      • 感谢阿希什的建议!澄清一下,该算法首先比较 j = 2 和 i = j - 1。
      【解决方案3】:

      感谢所有的答案。通过跟踪算法,我还发现在算法的第 5 行将第二个“大于”符号翻转为“小于”符号可以按降序工作。这可能是我正在阅读的书中的错字。再次感谢!

      【讨论】:

        【解决方案4】:

        试试这个方法

         public int[] insertionSort(int[] list) {
                    int i, j, key, temp;
                    for (i = 1; i < list.length; i++) {
                        key = list[i];
                        j = i - 1;
                        while (j >= 0 && key < list[j]) {
                            temp = list[j];
                            list[j] = list[j + 1];
                            list[j + 1] = temp;
                            j--;
                        }
        
                    }
                    return list;
                }
        

        【讨论】:

          【解决方案5】:
          package com.borntoinnovation.datastructure;
          
          import java.util.Arrays;
          
          public class SortingInsertion {
          
              public static void main(String[] args) {
                  int[] array = new int[] { 4, 3, 2, 20, 12, 1, 5, 6 };
          
                  for (int i = 0; i < array.length; i++) {
                      for (int j = 0; j < i; j++) {
                          if (array[i] < array[j]) {
                              int temp = array[i];
                              for (int k = i; k > j; k--) {
                                  array[k] = array[k - 1];
                              }
                              array[j] = temp;
                          }
                      }
                      System.out.println(Arrays.toString(array));
                  }
          
              }// end of main()
          }
          

          【讨论】:

          • 虽然此代码可能会解决问题,但 including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请编辑您的答案以添加解释,并说明适用的限制和假设。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多