Sort given array to descending | Java program to implement the above approach ; Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Sort last ( N - K ) array elements in ascending order ; Print array elements ; Driver Code
import java . util . * ; class GFG { static void sortArrayInDescAsc ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; for ( int i = 0 , j = N - 1 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } Arrays . sort ( arr , K , N ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 } ; int N = arr . length ; int K = 6 ; sortArrayInDescAsc ( arr , N , K ) ; } }