-1

My insertionSort Program not showing the correct result :'(

Arry is : 5 2 6 1 3

Result will: 1 2 3 5 6

But not Showing Result.

Code is:

public class insertionSort
{
    public static void main(String[] args)
    {
        int data[] ={5,2,6,1,3};
        for(int j = 2; j <data.length; j++)
        {
            int key = data[j];
            int i = j - 1;
            while(i > 0 && data[i] > key)
            {
                data[i + 1] = data[i];
                i=i-1;
            }

            data[i + 1] = key;
            System.out.print(data[j]+" ");


        }
    }
}
4

4 回答 4

1

尝试这个

 public class insertionSort
{
public static void main(String[] args)
{
    int data[] ={5,2,6,1,3};
    for(int j = 1; j <data.length; j++)
    {
        int key = data[j];
        int i = j - 1;
        while(i >= 0 && data[i] > key)
        {
            data[i + 1] = data[i];
            i=i-1;
        }

        data[i + 1] = key;


       System.out.print(data[j]+" ");
    }

}
}
于 2013-09-14T18:50:49.230 回答
0

除其他外,这里的主要问题似乎是您在对数组进行排序时尝试打印出数组的元素。让我们看一下循环的几个迭代,看看发生了什么:

j = 2; data = { 5, 2, 6, 1, 3 }
    key = data[2] = 6
    i = j - 1 ==> i = 2 - 1 = 1
    data[i] = 2 < key = 6 <-- while loop does nothing
    data[i+1] = data[2] = key = 6
    data[j] = 6

j = 3; data = { 5, 2, 6, 1, 3 }
    key = data[2] = 1
    i = j - 1 ==> i = 3 - 1 = 2
    data[i] = 6 > key = 1
        data[3] = data[2] ==> data[3] = 6
        i = i - 1 ==> i = 1
    data[i] = 2 > key = 1
        data[2] = data[1] ==> data[2] = 2
        i = i - 1 ==> i = 0
    i <= 0 <-- while loop stops
    data[i+1] = data[0] = key = 1
    data[j] = 6

data = { 5, 1, 2, 6, 3 }

在英语中发生的事情是插入排序将最大元素 (6) 保持在循环索引 j 处,因为这就是插入排序的工作方式。因此,每次循环迭代您只需打印出列表的当前最大元素,在这种情况下始终为 6。您还会在错误的索引处开始外循环(您应该从 1 开始,而不是 2,为什么从 2 开始?)。您也不会让您的内部循环查看数组的第一个元素。

所以你的代码应该看起来更像这样:

public class insertionSort
{
    public static void main( String[] args )
    {
        int data[] = { 5, 2, 6, 1, 3 };
        for( int j = 1; j < data.length; ++j )
        {
            int key = data[j];
            int i = j - 1;
            while( i >= 0 && data[i] > key )
            {
                data[i + 1] = data[i];
                --i;
            }

            data[i + 1] = key;
        }

        for( int k = 0; k < data.length; ++k )
        {
            System.out.print( data[k] + " " );
        }
    }
}
于 2013-09-14T19:41:43.083 回答
0

试试这样:

以开头for-loopj = 1并在 while 循环中创建条件i>=0

import java.util.Arrays;

public class insertionSort {
    public static void main(String[] args) {
        int data[] = { 5, 2, 6, 1, 3 };
        for (int j = 1; j < data.length; j++) {
            int key = data[j];
            int i = j - 1;
            while (i >= 0 && data[i] > key) {
                data[i + 1] = data[i];
                i = i - 1;
            }

            data[i + 1] = key;

        }
        System.out.println(Arrays.toString(data));
        // It prints [1, 2, 3, 5, 6]
    }
}
于 2013-09-14T18:58:30.880 回答
0

您可以为此问题创建单独的类。
并且真的在这个类上这个objectiv。

像这个:

public class InsertionSorter {
    private int[] a;

    /**
     * Constructs an insertion sorter.
     * @param anArray the array to sort
     */
    public InsertionSorter(int[] anArray) {
        a = anArray;
    }

    /**
     * Sorts the array managed by this insertion sorter
     */
    public void sort() {
        for (int i = 1; i < a.length; i++) {
            int next = a[i];

            // Move all larger elements up
            int j = i;
            while (j > 0 && a[j - 1] > next) {
                a[j] = a[j - 1];
                j--;
            }

            // Insert the element
            a[j] = next;
        }
    }
}

或者仅对现有类使用带有数组参数的静态方法。

于 2013-09-14T19:09:42.610 回答