#C3624. Sort Books by Popularity
Sort Books by Popularity
Sort Books by Popularity
You are given a series of test cases. In each test case, you are given the number of books and a list of their rating values. Your task is to sort the list of ratings in non-increasing order (i.e. the highest ratings come first) while preserving the original relative order among books with equal ratings. This means that if two books have the same rating, they should keep the same order as they appeared in the input.
Note: Use a stable sorting algorithm so that the order of equal elements remains unchanged.
Tip: In many programming languages the built-in sort function is stable; however, in some (like C++ and Java) you might have to ensure stability explicitly.
inputFormat
The input is read from standard input and has the following format:
T N1 r1 r2 ... rN1 N2 r1 r2 ... rN2 ... NT r1 r2 ... rNT
Here, T is the number of test cases. For each test case, the first line contains an integer N indicating the number of books, and the next line contains N integer ratings separated by spaces.
outputFormat
For each test case, output a single line containing the list of ratings sorted in non-increasing order with the order of equal ratings preserved. The ratings in each output line should be separated by a single space.
## sample2
5
5 2 9 9 5
4
1 3 2 2
9 9 5 5 2
3 2 2 1
</p>