<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>

int _ = [](){ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }();

class Solution {
public:
    vector<int> sortArray(vector<int>& nums) {
        struct impl {
            // Sorts [begin, tail] assuming [begin, tail) is non-empty and
            // already sorted.
            static void insert_tail(int* begin, int* tail) {
                __builtin_assume(begin != tail);
                auto val = *tail;
                for (auto p = tail; p != begin; tail = p) {
                    if (auto curr = *(p - 1); val < curr) {
                        *p-- = curr;
                    } else {
                        break;
                    }
                }
                *tail = val;
            }
            // Sorts [begin, end) assuming [begin, sorted_end) is non-empty
            // and already sorted.
            static void insertion_sort(int* begin, int* sorted_end, int* end) {
                for (; sorted_end != end; ++sorted_end) {
                    insert_tail(begin, sorted_end);
                }
            }

            // Sifts the non-root node at `idx` down the heap.
            static void sift_down(int* begin, size_t sz, size_t idx) {
                __builtin_assume(sz > 1);
                const auto tail = sz - 1;
                const auto val = begin[idx];

                auto child = idx * 2 + 1;
                int curr;
                while (curr = begin[child], child < tail) {
                    // Both children exist.
                    // Pick the larger one to compare with us.
                    auto next = child + 1;
                    auto right = begin[next];
                    if (__builtin_unpredictable(curr < right)) {
                        child = next;
                        curr = right;
                    }
                    if (val < curr) {
                        begin[idx] = curr;
                        idx = child;
                        child = idx * 2 + 1;
                        if (child <= tail) {
                            continue;
                        }
                    }
                    // No more larger children; leave.
                    begin[idx] = val;
                    return;
                }

                // Only 1 child. Conditionally swap it with our value.
                begin[idx] = __builtin_unpredictable(val < curr) ? curr : val;
                begin[child] = __builtin_unpredictable(val < curr) ? val : curr;
            }
            // Sorts [begin, begin + sz) assuming sz > 2.
            static auto heap_sort(int* begin, size_t sz) {
                for (unsigned i = sz / 2; !__builtin_sub_overflow(i, 1, &i); ) {
                    sift_down(begin, sz, i);
                }
                for (unsigned i = sz; --i != 1; ) {
                    swap(begin[0], begin[i]);
                    sift_down(begin, i, 0);
                }
                swap(begin[0], begin[1]);
            }

            // Same as `heap_sort` but uses the builtin algorithms.
            // (Our own algorithm seems to be faster.)
            static auto std_heap_sort(int* begin, size_t sz) {
                make_heap(begin, begin + sz);
                sort_heap(begin, begin + sz);
            }

            // The builtin introsort algorithm.
            static auto std_introsort(int* begin, size_t sz) {
                ranges::sort(begin, begin + sz);
            }
        };

        if (nums.size() >= 2) {
            impl::std_introsort(nums.data(), nums.size());
        }

        return move(nums);
    }
};

63623