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
def sortK ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT k = 3 NEW_LINE arr = [ 2 , 6 , 3 , 12 , 56 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE sortK ( arr , n , k ) NEW_LINE print ( " Following ▁ is ▁ sorted ▁ array " ) NEW_LINE printArray ( arr , n ) NEW_LINE