英文:
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<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 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("Original Array");
	    display(arr);
	    System.out.println("\nAfter Insertion Sort");
	    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"));
    }
}
Output :
Original Array                                                                                                                                                
34      54      12      32                                                                                                                                    
After Insertion Sort                                                                                                                                          
12      32      34      54 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论