1 # Include <stdio. h> 3 Void Shell_order ( Int *, Int Length) 4 { 5 Int Increment, I, j, TEM; 6 For (Increment = length/ 2 ; Increment> 0 ; Increment/= 2 ) 7 { 8 For (I = increment; I <length; I ++ ) 9 { 10 TEM = A [I]; 11 For (J = I; j> = increment; j-= Increment) 12 { 13 If (TEM <A [J- Increment]) 14 A [J] = A [J- Increment]; 15 Else Break ; 16 } 17 A [J] = TEM; 18 } 19 } 20 } 21 22 Int Main () 23 { 24 Int A [] = { 6 , 5 , 1 , 7 , 2 , 4 , 3 }; 25 Int Length = Sizeof ()/ Sizeof ( Int ); 26 Int I; 27 Shell_order (A, length ); 28 For (I = 0 ; I <length; I ++ ) 29 Printf ( " % D " , A [I]); 30 Printf ( " \ N " ); 31 Return 0 ; 32 }
This example Code About the implementation of hill sorting: first, select an incremental selection method, and then insert the sorted sorting data according to the incremental selection rules in each cycle, in essence, insert and sort independent sub-arrays.
Hill sorting: The Run Time of the hill sorting mainly depends on the selection of the incremental sequence. In the above example, the increment used in the Code is Shell's recommended H1 = [length/2]; h2 = [H1/2]... however, the incremental selection is not good, and the worst efficiency is O (n ^ 2). Below is an incremental selection:
(1) incremental selection of Hibbard: 1, 3, 7... 2 ^ K-1 ...; because there is no common factor in this incremental selection method, it is proved that the efficiency is O (n ^ (3/2 )); however, it is also said that after a large number of running such incremental selections, the efficiency is O (n ^ (5/4), but the efficiency is higher than the shell incremental efficiency;
(2) Another incremental selection proposed by Sedgewick: 9*4 ^ I-9 * 2 ^ I + 1 or 4 ^ I-3*4 ^ I + 1; using these two incremental methods, we can guess the average efficiency of shell sorting as O (N ^ (7/6), which is more efficient than the Hibbard method.
About Hill sorting: the performance of hill sorting is completely acceptable in practice, even for tens of thousands of N, due to simple programming features, this makes shell sorting commonly used to correspond to a large amount of data input. Algorithm .