<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:
    vector<int> sortArray(vector<int>& nums) {
        /**
            intuition: use merge sort to achieve O(nlog(n))
            - follow up: why is merge sort nlogn, what is the space complexity
           of the algorithm
        */
        // devide the array and sort them and merge them
        mergeSort(nums, 0, nums.size() - 1);
        return nums;
    }

private:
    void mergeSort(vector<int>& nums, int start, int end) {
        if (end == start) {
            return;
        }
        if (end == start + 1) {
            if (nums[start] > nums[end]) {
                const int temp = nums[start];
                nums[start] = nums[end];
                nums[end] = temp;
            }
            return;
        }
        // divide and sort left
        const int endOfLeft = (start + end) / 2;
        mergeSort(nums, start, endOfLeft);
        // divide and sort right
        mergeSort(nums, endOfLeft + 1, end);
        // merge left and right
        vector<int> temp;
        int leftIndex = start;
        int rightIndex = endOfLeft+1;
        int currentIndex = start;
        int tempValue;
        while(currentIndex <= end){

            if(leftIndex >endOfLeft){
                tempValue = nums[rightIndex];
                rightIndex++;
            }
            else if(rightIndex > end){
                tempValue = nums[leftIndex];
                leftIndex++;
            }
            else{
                if(nums[leftIndex] > nums[rightIndex]){
                    tempValue = nums[rightIndex];
                    rightIndex++;
                }
                else{
                    tempValue = nums[leftIndex];
                    leftIndex++;
                }
            }
            temp.push_back(tempValue);
            currentIndex++;
        }
        for(int i = 0; i < temp.size(); i++){
            nums[start+i] = temp[i];
        }
        return;
    }
};
212941