I. Thoughts
Insert sort: first, compare the size of the 2nd elements with the elements of the child array, starting from one element sub-array element, find a suitable location (and move the corresponding child array elements behind) and insert it. Then, compare the size of the 3rd elements with the child array (two elements) in sequence, insert it to the response position. In this way, the histogram sorts the entire array;
Ii. Comparison with selected sorting
Same: the elements on the left of the current index are ordered, but the final position of the inserted index is still unknown. In order to make room for smaller elements, it may move;
Different: the time required for inserting sorting depends on the initial order of the elements in the input (for large and array elements already ordered (or near-ordered) the sorting of arrays is much faster than that of arrays in random or reverse order;
Iii. Code
/*** Insert sorting ** @ author pengcx **/public class Insertion extends Sort {public static void main (String [] args) {String [] a = {"d", "a", "w", "B", "q"}; Insertion. sort (a); show (a);}/*** sort array a ** @ param a * sort array a */private static void sort (Comparable []) {int N =. length; for (int I = 0; I <N; I ++) {for (int j = I; j> 0 & less (a [j], a [j-1]); j --) {exch (a, j, j-1 );}}}}