easyArraysPattern: Sorting

Sort Integer Array Solution

Problem Statement

Given an array of integers `nums`, sort the array in ascending order.

Examples

Example 1:
Input:[4, 2, 7, 1, 3]
Output:[1, 2, 3, 4, 7]
Explanation: The array [4, 2, 7, 1, 3] sorted in ascending order is [1, 2, 3, 4, 7].
Example 2:
Input:[-5, 0, 10, -2, 8]
Output:[-5, -2, 0, 8, 10]
Explanation: The array [-5, 0, 10, -2, 8] sorted in ascending order is [-5, -2, 0, 8, 10].
Example 3:
Input:[5, 5, 5, 5]
Output:[5, 5, 5, 5]
Explanation: An array with all identical elements remains unchanged after sorting.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Time: O(n log n) Space: O(1) or O(log n) depending on the algorithm used by the built-in sort implementation
The most straightforward and efficient approach is to use a built-in sorting function provided by the language's standard library. These functions are typically implemented using optimized algorithms like Timsort (Python) or Introsort (C++), offering an average time complexity of O(n log n).

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

def sort_array(nums): nums.sort() return nums