1 // Binary semi-insertion sorting 2 /* Half insertAlgorithmThoughts 3 1. Initialization: Set the ordered area as the first element, and set the disordered area as all subsequent elements. 4 2. Sequentially retrieve each element in the unordered Area 5 3. Search for the ordered area using the binary method and return the maximum number smaller than this number. 6 4. retain data at this location 7 5. Move the elements from this position to the last element in the ordered area. 8 6. Fill the location with the reserved data 9 */ 10 Void Sortbyinserthalf ( Int Array [], Int Arraysize) 11 { 12 Int IMAX, imid, Imin; 13 For (Int I = 1 ; I <arraysize; I ++ ) 14 { 15 Int Temp = Array [I]; 16 IMAX = I- 1 ; 17 Imin = 0 ; 18 While (Imin <= IMAX) 19 { 20 Imid = (IMAX + Imin )/ 2 ; 21 If (Array [imid]> = Temp) 22 { 23 IMAX = imid- 1 ; 24 } 25 Else 26 { 27 Imin = imid + 1 ; 28 } 29 } 30 For ( Int J = I- 1 ; J> = IMAX +1 ; J -- ) 31 { 32 Array [J + 1 ] = Array [J]; 33 } 34 Array [IMAX + 1 ] = Temp; 35 } 36 } 37 38 Void Print ( Int A [], Int Length) 39 { 40 For ( Int I = 0 ; I <length; I ++ ) 41 { 42 Printf (" % D " , A [I]); 43 } 44 Printf ( " \ N " ); 45 } 46 47 Int Main () 48 { 49 Int A [ 10 ] = { 7 , 2 , 8 , 3 , 9 , 10 , 15 , 11 , 20 , 17 }; 50 Sortbyinserthalf (, Sizeof ()/ Sizeof ( Int )); 51 Print (, Sizeof ()/ Sizeof ( Int )); 52 Return 0 ; 53 }