Given an array of integers nums, sort the array in ascending order and return it.
You must solve the problem without using any built-in sorting functions, in O(n log n) time and with the smallest space complexity possible.
Function Signature
def sort_array(nums: list[int]) -> list[int]:
Rules
-
Return every element of
nums
in non-decreasing order. A value that appears several times in the input appears the same number of times in the output.
-
You may sort
nums
in place and return the same list.
-
Do not call a library sort or heap routine (
sorted
,
list.sort
,
heapq
or similar).
Constraints
-
1 <= len(nums) <= 5 * 10^4
-
-5 * 10^4 <= nums[i] <= 5 * 10^4
Examples
Input: nums = [5,2,3,1]
Output: [1,2,3,5]
The four distinct values come back in ascending order.
Input: nums = [5,1,1,2,0,0]
Output: [0,0,1,1,2,5]
Both copies of 0 and both copies of 1 are kept, so the output holds the same six elements as the input.
Input: nums = [3,-2,0,-2]
Output: [-2,-2,0,3]
Negative values sort below zero, and the repeated -2 appears twice.