Search⌘ K
AI Features

Solution: Sort an Array

Explore how to solve the array sorting problem efficiently by implementing the heap sort algorithm in Go. Understand building a max heap, repeatedly extracting the largest element, and sorting in place while maintaining optimal time and minimal extra space. This lesson helps you master in-place sorting techniques with guaranteed O(n log n) performance.

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