Search⌘ K
AI Features

Solution: Sort an Array

Explore how to implement heap sort to efficiently sort an integer array in ascending order with O(n log n) time complexity and minimal extra space. Understand the step-by-step process of building a max heap and repeatedly extracting the largest element to achieve in-place sorting. Learn to handle constraints typical in coding interviews and evaluate time-space tradeoffs for sorting algorithms.

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