简单的插入排序

我写了一个简单的插入排序程序,但输出不正确。

class InsertionSort{ public static void main(String h[]){ int[] a = {5,4,3,2,1}; int i,j,temp; for(i=1;i0 && a[j] > a[i]){ temp = a[i]; a[i] = a[j]; a[j] = temp; } } for(int x=0; x<a.length;x++){ System.out.println(a[x]); } } } 

 /*this program sort in ascending order by insertion sort */ class InsertionSort{ public static void main(String h[]){ int[] a = {100,12,31, 5, 4, 3, 2, 1 }; int i, j, temp; for (i = 1; i < a.length; i++) { j = i - 1; while (j >= 0 && a[j] > a[i] ) { temp = a[i]; a[i] = a[j]; a[j] = temp; i=j; j--; } } for(int x=0; x= 0 && a[j] < a[i] ) { temp = a[i]; a[i] = a[j]; a[j] = temp; i=j; j--; } } for(int x=0; x 

在外部循环的顶部,数组在元素i下面排序。 你不想把i移回arrays。 在内部循环中, j通过反复切换下一个,将从i开始的新元素向下移动到已排序的数组中。

 for (i = 1; i < a.length; i++){ for (j = i; j > 0 && a[j-1] > a[j]; j--){ temp = a[j]; a[j] = a[j-1]; a[j-1] = temp; } } 

插入排序就像扑克牌与朋友一样将它们添加到正确的位置。 我以OOP的方式实现了下面的代码试试看。

 public class Insertion_Sorting { private int[] A; public void read(int[] A){ this.A = A; } public int[] sort(){ for (int i = 1; i <= A.length-1; i++) { int element = A[i]; int j = i; while (j>0 && (A[j-1] > element)) { A[j] = A[j-1]; j--; } A[j] = element; } return A; } public void print(){ for (int i = 0; i < A.length; i++) { System.out.print(A[i] + " "); } } public static void main(String[] args){ int[] A = {23,42,4,16,8,15}; Insertion_Sorting I = new Insertion_Sorting(); I.read(A); I.sort(); I.print(); } 

}

 public static int[] doInsertionSort(int[] input) { int reverse; for (int i = 1; i < input.length; i++) { for (int j = i; j > 0; j--) { System.out.println("compare " + input[j - 1] + " to " + input[j]); if (input[j] < input[j - 1]) { reverse = input[j]; System.out.println("Reverse: "+ reverse); input[j] = input[j - 1]; input[j - 1] = reverse; new printNumbers(input); } } } return input; } printNumbers(int[] input) { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ", "); } System.out.println("\n"); } 
 public static void insertionsort(){ Scanner sc = new Scanner(System.in); int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 }; arr(input); } private static void printNumbers(int[] input) { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + ", "); } System.out.println("\n"); } public static void arr (int array[]){ int n = array.length; for (int j = 1; j < n; j++) { int key = array[j]; int i = j-1; while ( (i > -1) && ( array [i] > key ) ) { array [i+1] = array [i]; i--; } array[i+1] = key; printNumbers(array); } } 

插入排序

 public class InsertionSort { public static void main(String args[]){ int[] arry1 = {55,12,43,27,54,34,77,3,15,19}; int[] arry2 = insertionSort(arry1); System.out.println("Insertion Sort Demo:"); for(int i:arry2){ System.out.print(i); System.out.print(" "); } } public static int[] insertionSort(int[] arr){ int temp; for (int i = 1; i < arr.length; i++) { for(int j = i ; j > 0 ; j--){ if(arr[j] < arr[j-1]){ temp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = temp; } } } return arr; } } 

节目输出:

插入排序演示:

3 12 15 19 27 34 43 54 55 77

这是经过测试和运作的

 public static int[] insertionSort(int[] array) { int temp, j; for(int i = 1; i < array.length; i++) { temp = array[i]; for (j = i - 1; j >= 0; j--) { if (array[j] > temp) { array[j+1] = array[j]; } else { break; } } array[j + 1] = temp; } return array; } 
 Scanner s = new Scanner(System.in); System.out.println("Enter the nos"); num = s.nextInt(); System.out.println("Enter the "+num+" integers"); int array []= new int[num]; for(int count = 0;count=0 && array[j]>key) { array [j+1] = array[j]; j=j-1; } array[j+1] = key; } System.out.println("Sorted array"); for(int i=0;i 

插入排序是另一种排序数组元素的排序算法。该算法优于选择排序算法或冒泡排序算法。最坏情况时间复杂度和平均时间复杂度为(n ^ 2)。最佳情况时间复杂度为(n)。最坏的案例空间复杂度是(n),最好的案例空间复杂度是常数(1)。让我们看看如何实现插入排序算法。

 import java.util.Scanner; class insertion_sort{ public static void main(String a[]){ Scanner sc=new Scanner(System.in); System.out.print("Size of array :"); int n=sc.nextInt(); int[] num=new int[n]; int i,j,tmp,tmp2; for(i=0;itmp){ tmp2=num[ij-1]; num[ij-1]=tmp; num[ij]=tmp2; } else{ break; } } } for(i=0;i 

}

插入排序(ES6)

(如果有人需要)

 // insertion sort in-place function insertionSort(arr) { for (let i = 1; i < arr.length; i++) { for (let j = i; j > 0; j--) { if (arr[j] < arr[j - 1]) { [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]]; } else { break; } } } return arr; } 
 public int[] insertionSort(int[] list) { for (int i = 1; i < list.length; i++) { int key = list[i]; int j = i - 1; while (j >= 0 && key < list[j]) { int temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; j--; } } return list; } 

虽然这里不需要循环,

  if ((a[j] < a[i])) { temp = a[i]; a[i] = a[j]; a[j] = temp; }