Shell | Function to sort arr [ ] using Shell Metzner sort ; Set initial step size to the size of the array ; Step size decreases by half each time ; k is the upper limit for j ; j is the starting point ; i equals to smaller value ; l equals to larger value ; Compare and swap arr [ i ] with arr [ l ] ; Decrease smaller value by step size ; Increment the lower limit of i ; Function to print the contents of an array ; Driver code ; Sort the array using Shell Metzner Sort ; Print the sorted array
< ? php function sort_shell_metzner ( $ arr , $ n ) { $ m = $ n ; while ( $ m > 0 ) { $ m = $ m / 2 ; $ k = $ n - $ m ; $ j = 0 ; do { $ i = $ j ; do { $ l = $ i + $ m ; if ( $ arr [ $ i ] > $ arr [ $ l ] ) { $ temp = $ arr [ $ i ] ; $ arr [ $ i ] = $ arr [ $ l ] ; $ arr [ $ l ] = $ temp ; $ i -= $ m ; } else break ; } while ( $ i >= 0 ) ; $ j ++ ; } while ( $ j <= $ k ) ; } return $ arr ; } function printArray ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 0 , -2 , 8 , 5 , 1 ) ; $ n = count ( $ arr ) ; $ result_array = sort_shell_metzner ( $ arr , $ n ) ; printArray ( $ result_array , $ n ) ; ? >