<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> sorted_order(vector<int> v){
        // max len is 5
        // -9 -> 0, -1 -> 8, 0 -> 9, 9 -> 18
        int n = v.size();
        vector<int> ord(n); iota(ord.begin(), ord.end(), 0);
        vector<int> vx = v;
        for(int z=0; z<5; z++){
            vector<int> cnt(20);
            for(auto i: ord){
                int d = abs(v[i])%10;
                if(v[i]<0) d *= -1;
                cnt[d+9]++;
            }
            for(int i=1; i<20; i++) cnt[i] += cnt[i-1];
            vector<int> p(n);
            for(int idx=n-1; idx>=0; idx--){
                int i = ord[idx];
                int d = abs(v[i])%10;
                if(v[i]<0) d *= -1;
                p[--cnt[d+9]] = i;
                v[i] /= 10;
            }
            for(int i=0; i<n; i++) ord[i] = p[i];
        }
        return ord;
    }
    vector<int> sortArray(vector<int>& nums) {
        vector<int> ord = sorted_order(nums);
        int n = nums.size();
        vector<int> ans;
        for(auto i: ord){
            ans.push_back(nums[i]);
        }
        return ans;
    }
};
91403