Given an array of integers nums, your task is to sort the array in ascending order and return it.

Here's the catch: you must solve this problem without using any built-in sorting functions (like sort(), sorted(), etc.). Your solution must achieve O(n log n) time complexity and use the smallest space complexity possible.

This is a fundamental computer science problem that tests your understanding of sorting algorithms. You'll need to implement one of the classic sorting algorithms like Merge Sort, Quick Sort, or Heap Sort from scratch.

Example: Given [5, 2, 3, 1], you should return [1, 2, 3, 5]

Input & Output

example_1.py โ€” Basic Unsorted Array
$ Input: nums = [5,2,3,1]
โ€บ Output: [1,2,3,5]
๐Ÿ’ก Note: After sorting the array in ascending order, we get [1,2,3,5]. Each element finds its correct position through comparison and placement.
example_2.py โ€” Array with Duplicates
$ Input: nums = [5,1,1,2,0,0]
โ€บ Output: [0,0,1,1,2,5]
๐Ÿ’ก Note: The algorithm correctly handles duplicate values, maintaining their relative order and placing them in the proper sorted positions.
example_3.py โ€” Single Element Edge Case
$ Input: nums = [1]
โ€บ Output: [1]
๐Ÿ’ก Note: A single element array is already sorted by definition. The algorithm should handle this base case efficiently without unnecessary operations.

Visualization

Tap to expand
Sorting Algorithms: Time Complexity ComparisonBubble SortO(nยฒ)Simple but slowMany comparisonsSelection SortO(nยฒ)Easy to understandMinimum swapsMerge Sort โœ“O(n log n)Optimal & stableGuaranteed fastPerformance Comparison (Array Size vs Time)TimeArray SizeO(nยฒ) algorithmsO(n log n) merge sortWhy Merge Sort is Optimal:โœ“ Consistent O(n log n) performance regardless of inputโœ“ Stable sorting (maintains relative order of equal elements)โœ“ Divide-and-conquer approach scales well with large datasetsโœ“ Predictable memory usage O(n) for merging operations
Understanding the Visualization
1
Problem Setup
Given an unsorted array, we need to arrange elements in ascending order without using built-in functions
2
Choose Strategy
Simple O(nยฒ) algorithms work but are slow. Divide-and-conquer algorithms achieve required O(n log n)
3
Implement Merge Sort
Recursively divide array into halves until single elements, then merge back in sorted order
4
Merge Process
Use two pointers to efficiently combine two sorted arrays into one larger sorted array
Key Takeaway
๐ŸŽฏ Key Insight: Merge sort's divide-and-conquer strategy achieves the required O(n log n) time complexity by breaking the problem into smaller pieces that can be solved efficiently and combined optimally.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

Always makes n(n-1)/2 comparisons regardless of input

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only uses constant extra memory for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 5 ร— 104
  • -5 ร— 104 โ‰ค nums[i] โ‰ค 5 ร— 104
  • Must not use built-in sorting functions
  • Must achieve O(n log n) time complexity
Asked in
Google 85 Amazon 72 Meta 68 Microsoft 61 Apple 45
89.5K Views
Very High Frequency
~25 min Avg. Time
3.4K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen