Search⌘ K
AI Features

Solution: Sort an Array

Explore how to implement an efficient heap sort algorithm to sort an array of integers in ascending order. Understand the step-by-step process to build a max heap, perform in-place sorting, and achieve O(n log n) time complexity with minimal extra space. This lesson helps you apply greedy techniques for optimization and develop a solid grasp of sorting algorithms within coding interview contexts.

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