Search⌘ K
AI Features

Solution: Sort an Array

Understand how to implement heap sort to efficiently sort an integer array in ascending order. This lesson guides you through building a max heap, performing in-place sorting with minimal extra space, and analyzing time and space complexity to meet coding interview requirements.

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