Sort first half in ascending and second half in descending order | 1 | C ++ program to print first half in ascending order and the second half in descending order ; function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOrder ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) cout << arr [ i ] << " ▁ " ; for ( int j = n - 1 ; j >= n / 2 ; j -- ) cout << arr [ j ] << " ▁ " ; } int main ( ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , -1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printOrder ( arr , n ) ; return 0 ; }