13397 - DS_2021_HW5_Sort   

Description

In this homework, you are asked to re-sort a sorted array with our own comparison function.

Suppose A is a sorted array in increasing order. We say that A[i] <* A[j] if one of these statements is true:

  1. A[i]2 < A[j]2
  2. A[i]2 = A[j]2 and i < j

 

For example:

If A = [-8, -2, -1, 0, 1, 6, 10]

Then you should re-sort A as A* =  [0, -1, 1, -2, 6, -8, 10]

If A = [-795, -594, -356, -123, -46, -21]

Then you should re-sort A as A* = [-21, -46, -123, -356, -594, -795]

 

You are allowed to use STL.

But don’t use any STL related to sort.

Input

There are multiple test cases, and each test case begins with a line containing an integers n, which is the number of elements in the input.

The next line includes the n sorted integers (in increasing order) in the sequence.

Please note:

  1. 1 ≤ the number of elements ≤ 100,0000
  2. 1 ≤ value of each element ≤ 231-1.

Output

Output the re-sorted array in increasing order with our own comparison function.

In other words, if x <* y then x must appear before y.

Add a new line to each output.

Sample Input  Download

Sample Output  Download

Tags




Discuss