Back to DSA sheet

Sort an Array

MediumArrays

Sort the given array in ascending order. Aim for O(n log n) time. Here we use merge sort.

Input format (stdin): the first line has n. The second line has n integers. Output the sorted array, space separated.

Examples
Input: 5 5 2 3 1 4
Output: 1 2 3 4 5
Input: 3 5 1 1
Output: 1 1 5
Constraints
  • 1 <= n <= 5*10^4
  • -5*10^4 <= nums[i] <= 5*10^4
Sheets
NeetCode 250
sort-an-array.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.