Search⌘ K
AI Features

Solution: Sort an Array

Explore how to sort an integer array in ascending order efficiently by implementing heap sort, which guarantees O(n log n) time complexity and minimal space usage. Understand the heap construction, repeated extraction of the max element, and re-heapification to achieve an in-place sorting solution without relying on built-in sorting functions.

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 ...