Java 选择排序错误

Java 选择排序错误,java,arrays,sorting,Java,Arrays,Sorting,我从字段开始,有一个关于数组排序的问题。 我找不到程序没有数组排序的原因 谢谢你的帮助 代码 package sorting; public class selectionSort { public static void main (String[]args) { int []a={2,5,3,1,7,10,12}; printArray(a); insertSort(a); printArray(a);

我从字段开始,有一个关于数组排序的问题。 我找不到程序没有数组排序的原因 谢谢你的帮助

代码

package sorting;

public class selectionSort
{
    public static void main (String[]args)
    {
        int []a={2,5,3,1,7,10,12};

        printArray(a);
        insertSort(a);
        printArray(a);
    }

    public static void insertSort(int[]array)
    {   
        for(int i=0;i<array.length-1;i++)
        {
            int smallestIndex=i;
            for(int j=i+1;j<array.length;j++)
            {
                if(array[smallestIndex]>array[j])
                {
                    smallestIndex=j ;
                }
                if(smallestIndex!= i)
                {
                    int temp=array[smallestIndex];
                    array[smallestIndex]=array[i];
                    array[i]=temp;
                }
            }
        }
    }//insertSORT


    public static void printArray(int[] array)
    {
        for(int i=0 ;i<array.length;i++)
        {
            System.out.print(array[i]+" ");
        }
        System.out.println("");
    }

}
包裹分拣;
公共类选择排序
{
公共静态void main(字符串[]args)
{
int[]a={2,5,3,1,7,10,12};
打印阵列(a);
插入排序(a);
打印阵列(a);
}
公共静态void insertSort(int[]数组)
{   

对于(inti=0;i插入排序算法有两种方式,我的意思是你希望你的第一个for循环从左到右,第二个从右到左,从i到达的位置开始。 检查第一个for循环中的下一个数字,如果该数字小于要查找其所属位置的上一个数字,则将所有其他数字向右移动

for(int i = 1; i < A.length; i++){
        if(A[i]<A[i-1]){
            for(int j = i; j > 0; j--){
                do{
                    int x = A[j-1];
                    A[j-1] = A[j];
                    A[j] = x;
                } while (A[j]<A[j-1]);
            }
        }
    }
for(int i=1;i}而(A[j]此类排序的逻辑应为:

  • 找到数组中的最小数字,并将其放在第一位
  • 在数组的其余部分中找到最小的数字,并将其放在第二位
  • 在数组的其余部分中找到最小的数字,并将其放在第三位
等等

这里的问题是,您需要找到数组其余部分中最小的数字,只有在扫描完最小的数字后,才应该将该数字与第i个元素交换

但您的错误是,您在扫描时交换了值

那么,开始第一轮

┌───┬───┬───┬───┬───┬────┬────┐ │ 2 │ 5 │ 3 │ 1 │ 7 │ 10 │ 12 │ └───┴───┴───┴───┴───┴────┴────┘ 0 1 2 3 4 5 6 这很好-但您仍在扫描。您的
j
现在转到4,指向7。第一个
if
为非真。您不会更改
smallestIndex
。但是,当您到达第二个
if
时,它的值仍然是
3
。因此,您现在将再次交换:

┌───┬───┬───┬───┬───┬────┬────┐ │ 2 │ 5 │ 3 │ 1 │ 7 │ 10 │ 12 │ └───┴───┴───┴───┴───┴────┴────┘ 0 1 2 3 4 5 6
首先,选择排序和插入排序是两种不同的算法。您的方法名称有误导性

此处提供了有关其实施的信息:

┌───┬───┬───┬───┬───┬────┬────┐ │ 2 │ 5 │ 3 │ 1 │ 7 │ 10 │ 12 │ └───┴───┴───┴───┴───┴────┴────┘ 0 1 2 3 4 5 6
public static void insertSort(int[] array) {
    for (int i = 0; i < array.length - 1; i++) {
        int smallestIndex = i;
        for (int j = i + 1; j < array.length; j++) {
            if (array[smallestIndex] > array[j]) {
                smallestIndex = j;
            }
            // Not here!
        }
        // Do the swap outside the j loop.
        if (smallestIndex != i) {
            int temp = array[smallestIndex];
            array[smallestIndex] = array[i];
            array[i] = temp;
        }
    }
}