Skip to content
AI360Xpert
Beta
Difficulty: MediumSorting

Sort an Array

Problem in Plain English

Rearrange integer values into ascending order without calling a library sort. Compare Quick Sort, Merge Sort and Heap Sort by their time guarantees and memory use.

Problem Statement

Return nums in ascending order, preserving every value and its number of occurrences. Implement the sorting yourself rather than calling a built-in sorting function.

The target is O(nlog⁡n)O(n \log n) time with as little extra storage as possible. Randomized Quick Sort illustrates expected performance; Merge Sort and Heap Sort guarantee the time bound. Heap Sort is selected here because it also uses constant auxiliary space.

Constraints

  • 1 <= nums.length <= 50000
  • -50000 <= nums[i] <= 50000
  • Do not call a built-in sorting function.

Examples

Example 1

Input
nums = [5, 2, 3, 1]
Output
[1, 2, 3, 5]

The smallest value 1 moves to the front and the largest value 5 moves to the end. The middle values remain in ascending order.

Example 2

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

Both copies of 0 and both copies of 1 are retained. Sorting changes positions, not frequencies.

Example 3

Input
nums = [-50000]
Output
[-50000]

One element is already ordered. No partition, merge or heap extraction is needed.

Example 4

Input
nums = [2, -1, 2, -3, 0]
Output
[-3, -1, 0, 2, 2]

Negative values precede zero and positive values; the two equal 2 values occupy the last two positions.

Intuition

A quadratic scan repeatedly searches the entire unsorted part for its next value. Faster comparison sorts organize those comparisons: Quick Sort groups values around a pivot, Merge Sort combines runs already in order, and Heap Sort keeps the largest remaining value at a tree root. For [5, 2, 3, 1], all three produce [1, 2, 3, 5], but they preserve different invariants and use different storage. Stable sorting preserves the original relative order of equal-valued records; that matters for records with attached data, although equal integers are indistinguishable here.

Approaches

Three-Way Quick Sort

Solution Details

Reveal Three-Way Quick Sort: intuition, complexity, and code

Hints

Hint 1
Try finding the next smallest value repeatedly; why does rescanning cost quadratic time?
Hint 2
A pivot can classify values, or two sorted runs can be merged by comparing only their heads.
Hint 3
To get a worst-case time bound with constant extra storage, build a max heap inside nums and move its maximum to the end repeatedly.

Edge Cases

  • A singleton needs no sorting passes and returns unchanged.
  • All equal values form one Quick Sort equal region; no unequal recursive work remains.
  • Sorted and reverse-sorted input must not cause linear recursion depth; only the smaller Quick Sort side is recursive.
  • Merge Sort must handle a final run shorter than width and a missing right run.
  • Heap Sort must handle a parent with only a left child and restrict every repair to the active prefix.
  • Negative numbers and the extreme permitted values are compared directly; no offset or counting range is necessary.

Common Mistakes and Interview Tips

  • Advancing i after swapping a greater-than-pivot value with gt skips the incoming unknown value.
  • Keeping the pivot as a mutable array position rather than copying its value can change the comparison target during swaps.
  • Claiming randomized Quick Sort has a guaranteed O(n log n) bound; its worst case is still quadratic.
  • Recursing into both Quick Sort sides without controlling stack depth can overflow on pathological partitions.
  • Merging directly into the input without a buffer can overwrite an unread value. Taking the right head on equality also loses stability.
  • Using a min heap while placing extracted values at the end produces descending order.
  • Sifting into the fixed suffix after heap extraction breaks already placed maxima. The end index is the exclusive heap size.

Key Takeaway

Choose a sorting invariant to match the actual requirements: randomized partitions for expected speed, stable merging when equal-record order matters, or heap extraction for guaranteed O(n log n) time with constant auxiliary space.