Program to print an array in Pendulum Arrangement with constant space | Function to print the Pendulum arrangement of the given array ; Sort the array ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code
< ? php function pendulumArrangement ( $ arr , $ n ) { sort ( $ arr ) ; $ pos = $ n - 1 ; if ( $ n % 2 == 0 ) $ odd = $ n - 1 ; else $ odd = $ n - 2 ; while ( $ odd > 0 ) { $ temp = $ arr [ $ odd ] ; $ in = $ odd ; while ( $ in != $ pos ) { $ arr [ $ in ] = $ arr [ $ in + 1 ] ; $ in ++ ; } $ arr [ $ in ] = $ temp ; $ odd = $ odd - 2 ; $ pos = $ pos - 1 ; } $ start = 0 ; $ end = floor ( ( $ n - 1 ) / 2 ) ; for ( ; $ start < $ end ; $ start ++ , $ end -- ) { $ temp = $ arr [ $ start ] ; $ arr [ $ start ] = $ arr [ $ end ] ; $ arr [ $ end ] = $ temp ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) echo $ arr [ $ i ] , " ▁ " ; } $ arr = array ( 11 , 2 , 4 , 55 , 6 , 8 ) ; $ n = count ( $ arr ) ; pendulumArrangement ( $ arr , $ n ) ; ? >