Insert Sort: (we assume that the data column (D0,d1,d2,... DN), the sorted section is d0~di-1.
The starting element of the To sort section is Di (I is greater than or equal to 1 less than or equal to N), and the ordering process is as follows:
- Make k=0;
- When K<i, perform steps of
- Exit the loop operation when Dk>di
- K plus 1 (to compare to the next data)
- Bring the value of di into the TEMP variable W
- Dk~di Move backward One
- Bring the value of W into the DK
public class Sort {
public static void Main (string[] args) {
Int[] A = new int[10];
for (int i = 0; i < a.length; i++) {
int b = (int) (Math.random () * 100);
A[i] = b;
}
System.out.println ("Before sorting:");
for (int j = 0; J < A.length; J + +) {
System.out.print (a[j]+ "");
}
System.out.print ("\ n");
Insertsort (a);
System.out.println ("After sorting:");
for (int k = 0; k < a.length; k++) {
System.out.print (a[k]+ "");
}
}
public static int[] Insertsort (int[] b) {
for (int i=0;i<b.length;i++) {
int currentvalue = B[i];
int position = i;
for (int j=i-1;j>=0;j--) {
if (B[j]>currentvalue) {
B[J+1]=B[J];
Position-=1;
}else{
Break
}
}
B[position]=currentvalue;
}
return b;
}
}
Insert Sort method