Sort given array to descending | Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Print array elements ; Driver Code
function sortArrayInDescAsc ( arr , N , K ) { arr . sort ( ( b , a ) => a - b ) var temp = 0 ; for ( var i = K ; i < N ; i ++ ) { for ( var j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } arr . forEach ( element => { document . write ( element + " " ) ; } ) ; } var arr = [ 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 ] ; var N = arr . length ; var K = 6 ; sortArrayInDescAsc ( arr , N , K ) ;