Insert sort directly
Direct insertion sorting is a simple sorting method. The basic idea is to regard the first element of the original sequence as an ordered sequence at the beginning. Then, the second element of the original sequence and its subsequent elements are extracted in sequence, and inserted to the appropriate position in the sequence. This completes the sorting after N-1 inserts.
// Insert the sorting directly, x the array to be sorted, and n the array length is private static void sort (int [] x, int n) {int I, j, t; for (I = 1; I <n; I ++) {// I indicates the number of inserts, n-1-1 inserts t = x [I]; for (j = I-1; j> = 0; j --) {// find the location where t is to be inserted if (t <x [j]) {// if t <x [j], t is to be inserted before x [j]. x [j + 1] = x [j]; // x [j] is to be moved backward, so x [j + 1] = x [j]} else {break; // locate and exit the loop} x [j + 1] = t; // insert directly} // print the sorted array for (int m = 0; m <x. length; m ++) {System. out. println (x [m]);} return ;}