Loading content...
You are given an array of integers. Your task is to arrange all elements in ascending order and return the resulting array.
The challenge is to implement the ordering algorithm from scratch without relying on any built-in sorting utilities provided by the programming language. Your implementation must achieve a time complexity of O(n log n) and utilize as little additional space as possible.
This problem tests your understanding of efficient comparison-based sorting algorithms such as merge sort, quicksort, or heap sort, and your ability to implement them correctly and efficiently.
nums = [5,2,3,1][1,2,3,5]After arranging the elements in ascending order, the array becomes [1,2,3,5]. Notice that elements 2 and 3 remain in their relative positions since they were already in order, while 1 and 5 moved to their correct positions.
nums = [5,1,1,2,0,0][0,0,1,1,2,5]The array contains duplicate values. After ordering, all identical elements are grouped together: both 0s come first, followed by both 1s, then 2, and finally 5.
nums = [-3,2,-1,0,4,-2][-3,-2,-1,0,2,4]The array contains both positive and negative integers. After ordering, negative numbers appear first in ascending order (-3,-2,-1), followed by zero, and then positive numbers (2,4).
Constraints