Sort first k values in ascending order and remaining n | C ++ program to sort first k elements in increasing order and remaining n - k elements in decreasing ; function to sort the array ; Sort first k elements in ascending order ; Sort remaining n - k elements in descending order ; Driver code ; Our arr contains 8 elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOrder ( int arr [ ] , int n , int k ) { sort ( arr , arr + k ) ; sort ( arr + k , arr + n , greater < int > ( ) ) ; } int main ( ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , -1 } ; int k = 4 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printOrder ( arr , n , k ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }