Sort a nearly sorted array using STL | Given an array of size n , where every element is k away from its target position , sorts the array in O ( n Log n ) time . ; Sort the array using inbuilt function ; An utility function to print array elements ; Driver code
function sortK ( arr , n , k ) { ( arr ) . sort ( function ( a , b ) { return a - b ; } ) ; } function printArray ( arr , size ) { for ( let i = 0 ; i < size ; i ++ ) document . write ( arr [ i ] + " " ) ; document . write ( " " ) ; } let k = 3 ; let arr = [ 2 , 6 , 3 , 12 , 56 , 8 ] ; let n = arr . length ; sortK ( arr , n , k ) ; document . write ( " " ) ; printArray ( arr , n ) ;