Transferred from: http://blog.csdn.net/m13666368773/article/details/7506678
look at this picture I don't know if you can understand, in the insert sort, the array is divided into two types, "ordered array blocks" and "unordered array blocks", first time, extract a number 20 from the "unordered array block" as an ordered array block; the second time from the "unordered array block" Extract a number 60 ordered into the "ordered array block", that is, 20,60; third time in the same vein, the difference is that 10 is smaller than the value of an ordered array, so the 20,60 position moves back, freeing a position for 10 to insert, then you can insert all of them by this rule.
1 Packagecom.swust. Insert sort;2 3 Importcom.swust.utils.ArrayUtils;4 5 Public classExample1 {6 Public Static voidMain (string[] args) {7 int[] arr = Arrayutils.createarray (10);8 Arrayutils.showarray (arr);9 sort (arr);TenString string = ""; One String.intern (); A System.out.println (); - Arrayutils.showarray (arr); - } the Public Static voidSortint[] list) { - //No sequence required - for(inti = 1; i < list.length; i++) - { + inttemp = List[i];//10,11,33,1,3 - + intJ; A //If the extracted data is larger than the data in the current ordered array, simply insert it without moving the elements in the array at for(j = i-1; J >= 0 && temp < list[j]; j--) - { -List[j + 1] =List[j]; - } -LIST[J+1] =temp; - } in } -}
Direct insertion ordering of data structure learning