Sort a nearly sorted array using STL | A STL based Java program to sort a nearly sorted array . ; 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
import java . util . * ; public class GFG { static void sortK ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 2 , 6 , 3 , 12 , 56 , 8 } ; int n = arr . length ; sortK ( arr , n , k ) ; System . out . println ( " Following ▁ is ▁ sorted ▁ array " ) ; printArray ( arr , n ) ; } }