Sorting 2 - Partial Sort
Given an array \(A\) of \(N\) integers, sort the array such that the \(K\) largest elements of the array are put in the last \(K\) positions of the array in ascending order.
Note: The order of the unsorted elements are determined from swapping the \(K\) elements with the elements at their original positions.
Constraints
\(1 \le K \le N \le 10 ^ 4\)
\(1 \le A_{i} \le 10 ^ 5\)
Input Specification
The first line consists of \(N\) and \(K\), representing the size of the array \(A\) and the number of largest elements to sort at the end of the array respectively.
The next \(N\) lines will each consist of a single integer \(A_{i}\), representing the integer at index \(i\) in the array \(A\).
Output Specification
Output \(N\) lines, representing the array \(A\) with the \(K\) largest elements sorted at the end of the line. Each line should contain a single integer \(A_{i}\).
Sample Input
5 2
2
4
5
3
1
Sample Output
2
3
1
4
5
Explanation for Sample Input
The two largest elements of the array \([2,4,5,3,1]\) are 4
and 5
. We swap 5
with the last element of the array and 4
with the second last element of the array, leaving us with \([2,3,1,4,5]\).
Comments