Package WZS. sort; import Java. util. arrays; // Insert the sorting directly. // basic idea: In the number of groups to be sorted, assume that the preceding (n-1) [n> = 2] The number is already in the correct order. Now we need to insert the nth number into the preceding order number so that the N number // is in the correct order. This repeats until all the rows are sorted. Public class test_wzs003 {public static void main (string [] ARGs) {int intarray [] = {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}; insertsort (intarray);}/*** insert sort * @ Param intarray */Private Static void insertsort (INT [] intarray) {int temp = 0; for (INT I = 1; I <intarray. length; I ++) {temp = intarray [I]; Int J = I-1; for (; j> = 0 & temp <intarray [J]; j --) {intarray [J + 1] = intarray [J];} intarray [J + 1] = temp; system. out. println ("+ I +" Times: "+ arrays. tostring (intarray);} system. out. print ("\ n:" + arrays. tostring (intarray ));}}
Output result:
1st sorting: [3, 10, 5, 7, 9, 1, 4, 2, 6, 8] 2nd sorting: [3, 5, 10, 7, 9, 1, 4, 2, 6, 8] 3rd sorting: [3, 5, 7, 10, 9, 1, 4, 2, 6, 8] 4th sorting: [3, 5, 7, 9, 10, 1, 4, 2, 6, 8] 5th sorting: [1, 3, 5, 7, 9, 10, 4, 2, 6, 8] 6th sorting: [1, 3, 4, 5, 7, 9, 10, 2, 6, 8] 7th sorting: [1, 2, 3, 4, 5, 7, 9, 10, 6, 8] 8th sorting: [1, 2, 3, 4, 5, 6, 7, 9, 10, 8] 9th sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] After sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]