Skip to main content
Back to problems
Leetcode
Medium
Arrays
Sorting
Divide and Conquer
Sort an Array

Sort an integer array in non-decreasing order.

Acceptance 88%
Problem Statement

Given an integer array nums, reorder the elements so that they appear in non-decreasing order.

Return the sorted array.

Your solution should be efficient enough for large inputs and should not rely on language-provided one-line sorting as the core idea for interview practice.

Input Format

  • An integer array nums.
  • The array may contain duplicate values and negative numbers.

Output Format

  • Return the same values arranged in non-decreasing order.

Constraints

  • 1 <= nums.length
  • Elements may be negative, zero, or positive.
  • Duplicate values may appear.
  • Aim for better than quadratic time in typical interview solutions.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [5,2,3,1]

Output

[1,2,3,5]

Explanation

The numbers are reordered from smallest to largest.

Example 2

Input

nums = [5,1,1,2,0,0]

Output

[0,0,1,1,2,5]

Explanation

Duplicates are preserved and placed next to equal values in the final ordering.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.