Sort an array in wave form | A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] ... . ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; driver code
function swap ( arr , a , b ) { let temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } function sortInWave ( arr , n ) { for ( let i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( arr , i - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } } let arr = [ 10 , 90 , 49 , 2 , 1 , 5 , 23 ] ; let n = arr . length ; sortInWave ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ;