The basic operation of the direct insertion sort (Straight insertion sort) is to insert a record into an ordered table, resulting in a new ordered table with a record plus 1.
The implementation code is given below:
public void Insertsort (int a[]) {
int i, J;
for (i = 2; i < a.length i++) {
if (A[i] < a[i-1]) { //If the latter value is less than the previous value, insert the small value a[i] into the ordered child table
a[0] = a[i];
//Set Sentry for
(j = i-1; A[j] > A[0]; j--) {
a[j + 1] = A[j]; Record post
}
a[j + 1] = a[0]; Insert to correct location
}
}
Test code:
public static void Main (String args[]) {
int[] arr= new int[]{0, 5, 3, 4, 6, 2};<span style= "White-space:pre" >< C16/></span>//0 adds
new Insertsort () Insertsort (arr)
to set up sentries. for (int i=1;i<arr.length;i++) {
System.out.print (arr[i]+ "");
}
System.out.println ();
}
Test results: