<p>Given an array of integers <code>nums</code>, sort the array in ascending order and return it.</p>

<p>You must solve the problem <strong>without using any built-in</strong> functions in <code>O(nlog(n))</code> time complexity and with the smallest space complexity possible.</p>

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

<pre>
<strong>Input:</strong> nums = [5,2,3,1]
<strong>Output:</strong> [1,2,3,5]
<strong>Explanation:</strong> After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
</pre>

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

<pre>
<strong>Input:</strong> nums = [5,1,1,2,0,0]
<strong>Output:</strong> [0,0,1,1,2,5]
<strong>Explanation:</strong> Note that the values of nums are not necessairly unique.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>-5 * 10<sup>4</sup> &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li>
</ul>

class Solution {
private:
    void Merge(vector<int>& array, int m, int l, int r){
        if (l >= r || m < l || m > r) return;
        if (r == l + 1 && array[l] > array[r]) {
            swap(array[l], array[r]);
            return;
        }
        vector<int> tmp(&array[l], &array[r] + 1);
        // for(auto i : tmp){
        //     cout << i << " ";
        // }
        // cout << "\n";
        for (int i = l, j = 0, k = m - l + 1; i <= r; i++){
            if (j > m - l){
                array[i] = tmp[k++];
            }
            else if(k > r - l){
                array[i] = tmp[j++]; 
            }
            else{
                array[i] = (tmp[j] < tmp[k]) ? tmp[j++] : tmp[k++];
            }
        }
    }

    void MergeSort(vector<int>& A, int first, int last) {
        if (first < last) {
            int m = (first + last) / 2;
            MergeSort(A, first, m);  //сортировка левой части
            MergeSort(A, m + 1, last);  //сортировка правой части
            Merge(A, m, first, last);  //слияние двух частей
        }
    }
    
public:
    vector<int> sortArray(vector<int>& nums) {
        MergeSort(nums, 0, nums.size() - 1);
        return nums;
    }
};
140018