<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 {
public:
    void mergeTwoArrays(int s, int e, vector<int>& nums) {
        int mid = (s+e)/2;
        int n = mid-s+1;
        int m = e-mid;
        vector<int>v1(n);
        vector<int>v2(m);

        int ct = s;
        for(int i=0 ; i<n ; i++){
            v1[i] =  nums[ct++];
        }
        int ct2 = mid+1;
        for(int j=0 ; j<m ;j++){
            v2[j] = nums[ct2++];
        }
        int i=0  , j= 0 ;
        vector<int>ans(n+m);
        int k = 0 ;
        while(i<n and j<m){
            if(v1[i] <= v2[j]){
                ans[k] = v1[i];
                i++,k++;
            }else{
                ans[k] = v2[j];
                j++,k++;
            }
        }
        while(i<n){
            ans[k] = v1[i];
            i++,k++;
        }

        while(j<m){
            ans[k] = v2[j];
            j++,k++;
        }

        for(int p=0; p<n+m ; p++){
            nums[s+p] = ans[p];
        }
    }
    
    void mergeSort(int s, int e, vector<int>& nums) {
        if (s < e) {
            int mid = s + (e - s) / 2;
            
            mergeSort(s, mid, nums);
            mergeSort(mid + 1, e, nums);
            mergeTwoArrays(s, e, nums);
        }
    }
    
    vector<int> sortArray(vector<int>& nums) {
        mergeSort(0, nums.size() - 1, nums);
        return nums;
    }
};

247666