Search⌘ K
AI Features

Solution: Sort an Array

Understand how to apply heap sort to sort an array efficiently in ascending order. Explore building a max heap, repeatedly extracting the largest element, and maintaining heap properties for in-place sorting with optimal time and space complexity.

Statement

Given an integer array, nums, sort it in ascending order and return the sorted array.

You must implement the sorting algorithm yourself; do not use any built-in sorting functions. The solution must run in O(nlogn)O(n \log n) time and use the minimum possible extra space.

Constraints:

  • 11 \leq nums.length ...