<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(int s,int mid,int e,vector<int> &nums){
        vector<int> temp(e-s+1,0);
        int i = 0,j = mid+1,k= s;
        while(s<=mid && j<=e){
            if(nums[s]<nums[j])temp[i++]  = nums[s++];
            else temp[i++] = nums[j++];
        }
        while(s<=mid)temp[i++] = nums[s++];
        while(j<=e)temp[i++] = nums[j++];
        for(int i=k;i<=e;i++)
            nums[i] = temp[i-k];
    }
    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);
        merge(s,mid,e,nums);
       }
    }
    int partition(int s,int e,vector<int> &nums){
        int pivot = nums[s],i = s,j = e;
       while(i<j){
        while(nums[i]<=pivot && i<e)i++;
        while(nums[j]>pivot && j>s) j--;
        if(i<j)swap(nums[i],nums[j]);
       }
       swap(nums[s],nums[j]);
       return j;
    }
    void quickSort(int s,int e,vector<int> &nums){
        // cout<<s<<" "<<e<<"\n";
        if(s<e){
            int pivot = partition(s,e,nums);
            quickSort(s,pivot-1,nums);
            quickSort(pivot+1,e,nums);
        }
    }
    bool isSorted(int &n,vector<int> &nums){
        for(int i = 0;i<n-1;i++){
            if(nums[i]>nums[i+1])return 0;
        }
        return 1;
    }
public:
    vector<int> sortArray(vector<int>& nums) {
        int n = nums.size();
        if(isSorted(n,nums))return nums;
        // quickSort(0,n-1,nums);
        mergeSort(0,n-1,nums);
        return nums;
    }
};
133073