插入排序错误输出

huangapple go评论77阅读模式
英文:

Insertion Sort Wrong output

问题

我已经实现了一个插入排序算法,但在显示数组时会出现错误的输出。

public void insertionSort() {
    int[] arr = new int[]{34, 54, 12, 32};

    for (int i = 1; i < arr.length; i++) {
        int key = arr[i];
        int hole = i - 1;

        while (hole >= 0 && key < arr[hole]) {
            arr[i + 1] = arr[i];
            hole -= 1;
        }
        arr[hole + 1] = key;
    }
    display(arr);
}

public void display(int[] dis) {
    Arrays.stream(dis).forEach(v -> System.out.print(v + "\t"));
}

output

12  54  12  12

我找不到问题出在哪里。

英文:

I have implemented a insertion Sort algorithm and when i display the array wrong output will be displayed.

public void insertionSort()
    {
        int[] arr=new int[]{34,54,12,32};

        for(int i=1;i&lt;arr.length;i++)
        {
            int key=arr[i];
            int hole=i-1;

            while(hole&gt;=0 &amp;&amp; key&lt;arr[hole])
            {
                arr[i+1]=arr[i];
                 hole-=1;
            }
            arr[hole+1]=key;
        }
        display(arr);

    }

  public void display(int[] dis)
    {
        Arrays.stream(dis).forEach(v-&gt;System.out.print(v+&quot;\t&quot;));
    }

output

12	54	12	12	

i could not find what is wrong

答案1

得分: 1

你的条件需要改变以适应 while 循环:你之前在 i 上进行了修改,而你应该在 hole 上进行修改。

完整代码

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        int[] arr = new int[]{34, 54, 12, 32};
        System.out.println("原始数组");
        display(arr);
        System.out.println("\n插入排序后");
        insertionSort(arr);
        display(arr);
    }
    
    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int key = arr[i];
            int hole = i - 1;

            while (hole >= 0 && arr[hole] > key) {
                arr[hole + 1] = arr[hole];
                hole -= 1;
            }
            arr[hole + 1] = key;
        }
    }

    public static void display(int[] dis) {
        Arrays.stream(dis).forEach(v -> System.out.print(v + "\t"));
    }
}

输出:

原始数组
34      54      12      32      
插入排序后
12      32      34      54
英文:

Your conditions needs to be changed for the while loop : You were modifying on i rather u should modify on hole

Full Code

import java.util.*;

public class Main {
    
	public static void main(String[] args) {
		int[] arr=new int[]{34,54,12,32};
	    System.out.println(&quot;Original Array&quot;);
	    display(arr);
	    System.out.println(&quot;\nAfter Insertion Sort&quot;);
	    insertionSort(arr);
	    display(arr);
	}
	public static void insertionSort(int[] arr) {
        for(int i=1;i&lt;arr.length;i++) {
            int key=arr[i];
            int hole=i-1;

            while(hole&gt;=0 &amp;&amp; arr[hole]&gt;key) {
                arr[hole+1]=arr[hole];
                 hole-=1;
            }
            arr[hole+1]=key;
        }
    }

    public static void display(int[] dis)  {
        Arrays.stream(dis).forEach(v-&gt;System.out.print(v+&quot;\t&quot;));
    }
}

Output :

Original Array                                                                                                                                                
34      54      12      32                                                                                                                                    
After Insertion Sort                                                                                                                                          
12      32      34      54 

huangapple
  • 本文由 发表于 2020年10月4日 19:15:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64193919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定