<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing</strong> order, return <em>an array of <strong>the squares of each number</strong> sorted in non-decreasing order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-4,-1,0,3,10]
<strong>Output:</strong> [0,1,9,16,100]
<strong>Explanation:</strong> After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-7,-3,2,3,11]
<strong>Output:</strong> [4,9,9,49,121]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code><span>1 &lt;= nums.length &lt;= </span>10<sup>4</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Squaring each element and sorting the new array is very trivial, could you find an <code>O(n)</code> solution using a different approach?
class Solution {
public:
    void MergeSort(vector<int> & nums, int from, int to) {
        vector<int> result(to-from+1, 0);
        int mid = (from+to)/2;
        int i=0,j=0;
        int curr = 0;
        if (from >= to) return ;
        MergeSort(nums, from, mid);
        MergeSort(nums, mid+1, to);
        i = from;
        j = mid+1;
        curr = 0;
        while (i <= mid && j <=to) {
            if (nums[i] < nums[j]) {
                result[curr++] = nums[i];
                i++;
            } else {
                result[curr++] = nums[j];
                j++;
            }
        }
        while (i<=mid) {
            result[curr++] = nums[i];
            i++;
        }
        while (j<=to) {
            result[curr++] = nums[j];
            j++;
        }
        for (int i=from; i<=to; i++) {
            nums[i] = result[i-from];
        }
    }

    void QuickSort(vector<int> &nums, int from, int to) {
        if (from >= to) {return;}

        int pilot = nums[from];
        int lessIdx = from-1;
        for (int i=from+1; i<=to; i++) {
            if (nums[i] <= pilot) {
                nums[++lessIdx] = nums[i];
            }
        }
        nums[++lessIdx] = pilot;
        QuickSort(nums, from, lessIdx-1);
        QuickSort(nums, lessIdx+1, to);
    }

    // 需要构建一个大顶堆，用于维护大小关系
    void heapify(vector<int> &nums, int to, int idx) {
        int left = 2*idx;
        int right = 2*idx+1;
        int maxIdx = idx;
        if (left < to && nums[left] > nums[maxIdx]) {
            maxIdx = left;
        }
        if (right < to && nums[right] > nums[maxIdx]) {
            maxIdx = right;
        }

        if (maxIdx != idx) {
            swap(nums[maxIdx], nums[idx]);
            heapify(nums, to, maxIdx);
        }
    }
    void HeapSort(vector<int> &nums) {
        vector<int> oneIdx(nums.size()+1, 0);
        for (int i=0; i<nums.size(); i++) {
            oneIdx[i+1] = nums[i];
        }
        for (int i=oneIdx.size()/2; i>0; i--) {
            heapify(oneIdx, oneIdx.size(), i);
        }
        for (int i=oneIdx.size()-1; i>1; i--) {
            swap(oneIdx[1], oneIdx[i]);
            heapify(oneIdx, i, 1);
        }
        for (int i=0; i<nums.size(); i++) {
            nums[i] = oneIdx[i+1];
        }
    }

    vector<int> sortedSquares(vector<int>& nums) {
        int i=0;
        for (i=0; i<nums.size(); i++) {
            nums[i] = nums[i]*nums[i];
        }
        // Bubble Sort
        // for (i=nums.size()-1; i>0; i--) {
        //     for (int j=0; j<i; j++) {
        //         if (nums[j] > nums[i]) {
        //             swap(nums[j], nums[i]);
        //         }
        //     }
        // }
        // Selection Sort
        // for (i=nums.size()-1; i>0; i--) {
        //     int maxIdx = i;
        //     for (int j=0; j<i; j++) {
        //         if (nums[j] > nums[maxIdx]) {
        //             maxIdx = j;
        //         }
        //     }
        //     swap(nums[maxIdx], nums[i]);
        // }
        // Insertion Sort
        // for (i=1; i<nums.size(); i++) {
        //     for (int j=i-1; j>=0; j--) {
        //         if (nums[j+1] < nums[j]) {
        //             swap(nums[j+1], nums[j]);
        //         } else {
        //             break;
        //         }
                 
        //     }
        // }
        // Merge Sort
        // MergeSort(nums, 0, nums.size()-1);

        // Quick Sort
        //QuickSort(nums, 0, nums.size()-1);

        // Heap Sort
        HeapSort(nums);
        for (i=0; i<nums.size(); i++) {
            std::cout << "[" << nums[i] <<"] "  ;
        }
        std::cout << std::endl;

        return nums;
    }
};
29100