Sort a nearly sorted array using STL | A STL based C ++ 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 program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sortK ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int k = 3 ; int arr [ ] = { 2 , 6 , 3 , 12 , 56 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortK ( arr , n , k ) ; cout << " Following ▁ is ▁ sorted ▁ array STRNEWLINE " ; printArray ( arr , n ) ; return 0 ; }