简体   繁体   English

Java 插入排序中的错误

[英]Error in Java Insertion Sort

I've attempted to code an Insertion Sort in java via pseudo code from a book.我试图通过书中的伪代码在 java 中编写插入排序。 The output for this code should be numbers in ascending order but for some reason I get 10,4,5,6,7,8,9.此代码的输出应该是按升序排列的数字,但由于某种原因,我得到了 10、4、5、6、7、8、9。 Any help is greatly appreciated!任何帮助是极大的赞赏! Thanks!谢谢!

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]);
        }
    }
}

Start the for loop from j=1 like this :像这样从 j=1 开始 for 循环:

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

and modify while loop condition like this:并像这样修改 while 循环条件:

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

Correct working code :正确的工作代码:

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] + " ");
    }
}

}

At the third line of your code ie,在代码的第三行,即

InsertionSort obj1 = new InsertionSort();

You are making an object of InsertionSort class but in your code, it is defined as a function I think it is a constructor you must mention the class for more convenience of the reader.您正在创建 InsertionSort 类的对象,但在您的代码中,它被定义为一个函数,我认为它是一个构造函数,您必须提及该类以方便读者。

Apart from that, you are starting your loop with 2 for(int j = 2; j < Array.length; j++) why so?除此之外,您以 2 for(int j = 2; j < Array.length; j++)开始循环,为什么会这样? your one element got missed so start j with 1 :)你错过了一个元素,所以从 1 开始 j :)

Thanks to all the answers.感谢所有的答案。 I have also found by tracing the algorithm that flipping the second 'greater than' sign to a 'less than' sign on the 5th line of the algorithm does the trick for working in descending order.我还通过跟踪算法发现将第二个“大于”符号翻转到算法第 5 行的“小于”符号可以实现按降序工作的技巧。 This may possibly be a typo in the book i'm reading.这可能是我正在阅读的书中的错字。 Thanks again!再次感谢!

try this method试试这个方法

 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;
        }
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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM