Package datastructure.maskedmen.sort;
Import Java.util.Arrays;
/**
* @author Maskedmen
* Direct insertion sequencing time complexity: O (n2)
*/
public class Insertsort {
public static void Main (string[] args) {
Int[] array = {15,6,2,9,5,10,3,18,1};
System.out.println ("The original array is:" +arrays.tostring (array));
Insertsort (array,array.length);
System.out.println ("Sorted array is:" +arrays.tostring (array));
}
Direct Insert Sort
public static void Insertsort (int[] array,int N) {
int i=0, j=0,temp=0;
Compare N-1 Wheels
for (i = 1; i < n; i++) {
temp = array[i];//takes out an unordered element
Find the place to insert in the sorted sequence
for (j = i-1; J >= 0 && temp < array[j];--j) {
ARRAY[J+1] = array[j];//element moves backwards
}
Array[j + 1] = temp;//Insert element to series
}
}
}
This article is from the "maskedmen" blog, make sure to keep this source http://maskedmen.blog.51cto.com/8100866/1745350
Java Direct Insert Sort