Sort first k values in ascending order and remaining n | Java 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
import java . util . * ; public class SortExample { static void printOrder ( Integer [ ] arr , int k ) { int n = arr . length ; Arrays . sort ( arr , 0 , k ) ; Arrays . sort ( arr , k , n , Collections . reverseOrder ( ) ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 } ; int k = 4 ; printOrder ( arr , k ) ; System . out . printf ( " % s " , Arrays . toString ( arr ) ) ; } }