Fundamentals/Intro to Sorting
← PrevNext →
Sort an integer array in ascending order. The implementation choice is yours: you may use a built-in sort function, or hand-roll any algorithm you like (bubble sort, selection sort, insertion sort, merge sort, quicksort, etc.). The only requirement is that the returned array contains exactly the same integers as the input, arranged from smallest to largest.
Return a new sorted array; do not mutate the input.
Input
unsortedList: an array of integers (may be empty)
Output
A new array containing the same integers sorted in ascending order.
Examples
Example 1
InputunsortedList = [1, 2, 3]
Output[1, 2, 3]
Explanation: The input is already sorted, so the output matches it.
Example 2
InputunsortedList = [1]
Output[1]
Explanation: A single-element array is trivially sorted.
Constraints
0 <= unsortedList.length
Elements are integers.
The input array must not be mutated.
Any correct sorting approach is acceptable.