The direct insertion algorithm is to look at N-sorted elements into an ordered table and an unordered table.
Each time an element is taken from an unordered table and an ordered table is compared, the sorting is repeated N-1 times.
Directly on the code:
1 Packagetest;2 3 Public classTest {4 Public Static voidMain (string[] args) {5 int[] data =New int[] {23, 1, 5, 7, 45, 26, 77, 3, 2 };6 Showarray (data);7 Sortarray (data);8 Showarray (data);9 }Ten One //Direct Insert Sort A Public Static voidSortarray (int[] data) { - for(inti = 1; i < data.length; i++) { - inttemp =Data[i]; the if(Data[i] < data[i-1]) { - intj = I-1; - - while(J >= 0 && Temp <Data[j]) { +Data[j + 1] =Data[j]; -j--; + } AData[j + 1] =temp; at } - } - } - //iterating over array elements - Public Static voidShowarray (int[] data) { - for(inti = 0; i < data.length; i++) { inSystem.out.print (Data[i] + ""); - } to System.out.println (); + } -}
Java Direct Insertion algorithm