Program to print an array in Pendulum Arrangement with constant space | Function to print the Pendulum arrangement of the given array ; Sort the array sort ( arr , arr + n ) ; ; 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
function pendulumArrangement ( arr , n ) { arr . sort ( function ( a , b ) { return a - b } ) ; let odd , temp , p , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; p = odd ; while ( p != pos ) { arr [ p ] = arr [ p + 1 ] ; p ++ ; } arr [ p ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } let start = 0 , end = parseInt ( ( n - 1 ) / 2 , 10 ) ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } let arr = [ 11 , 2 , 4 , 55 , 6 , 8 ] ; let n = arr . length ; pendulumArrangement ( arr , n ) ;