Sort an Array
Given an integer array nums, sort it in ascending (non-decreasing) order and return the sorted array.
You may not call a built-in sort — implement the sorting yourself. This problem exists to exercise a comparison-based sort (quicksort, mergesort, heapsort, …) from scratch rather than to test whether you know a library function.
Example cases
- unsortedin nums = [5,2,3,1]out [1,2,3,5]
- duplicatesin nums = [5,1,1,2,0,0]out [0,0,1,1,2,5]Repeated values (1s and 0s) stay adjacent in the sorted output.
- negativesin nums = [-3,0,-1,2,-2]out [-3,-2,-1,0,2]Negative and non-negative values sort together on one number line.
Constraints
- 1 <= nums.length <= 5 * 10^4
- -5 * 10^4 <= nums[i] <= 5 * 10^4
nums =
[5,2,3,1]