Program to print an array in Pendulum Arrangement | Prints pendulam arrangement of arr [ ] ; sorting the elements ; Auxiliary array to store output ; calculating the middle index ; storing the minimum element in the middle i is index for output array and j is for input array . ; adjustment for when no . of elements is even ; Printing the pendulum arrangement ; input Array ; calculating the length of array A ; calling pendulum function
< ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr , $ n ) ; sort ( $ arr ) ; $ op [ $ n ] = NULL ; $ mid = floor ( ( $ n - 1 ) / 2 ) ; $ j = 1 ; $ i = 1 ; $ op [ $ mid ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i <= $ mid ; $ i ++ ) { $ op [ $ mid + $ i ] = $ arr [ $ j ++ ] ; $ op [ $ mid - $ i ] = $ arr [ $ j ++ ] ; } if ( $ n % 2 == 0 ) $ op [ $ mid + $ i ] = $ arr [ $ j ] ; echo " Pendulum ▁ arrangement : " ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ op [ $ i ] , " ▁ " ; echo " STRNEWLINE " ; } arr = array ( 14 , 6 , 19 , 21 , 12 ) ; $ n = sizeof ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >