Sort even | Javascript Program to separately sort even - placed and odd placed numbers and place them together in sorted array . ; create evenArr [ ] and oddArr [ ] ; Put elements in oddArr [ ] and evenArr [ ] as per their position ; sort evenArr [ ] in ascending order sort oddArr [ ] in descending order ; Driver code
function bitonicGenerator ( arr , n ) { let evenArr = [ ] ; let oddArr = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( i % 2 != 1 ) { evenArr . push ( arr [ i ] ) ; } else { oddArr . push ( arr [ i ] ) ; } } evenArr . sort ( function ( a , b ) { return a - b ; } ) ; oddArr . sort ( function ( a , b ) { return b - a ; } ) ; let i = 0 ; for ( let j = 0 ; j < evenArr . length ; j ++ ) { arr [ i ++ ] = evenArr [ j ] ; } for ( let j = 0 ; j < oddArr . length ; j ++ ) { arr [ i ++ ] = oddArr [ j ] ; } } let arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] ; let n = arr . length ; bitonicGenerator ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { document . write ( arr [ i ] + " " ) ; }