Heap Sort (Implementation)
Priority Queue / HeapifySolved Nov 18, 2025
Asked at 10 companies
5 recent
- Bloomberg
- Amazon
- Microsoft
- Meta
- Hive
- Infosys
- tcs
- Oracle
- TikTok
Community-reported · source · updated 2026-10-08. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Every “Sort by” button: Sorting a spreadsheet by price, then by name, needs a stable sort, which is why Python and JavaScript both use merge-sort-based TimSort.
ProblemProblem statementGiven an array of integers, sort the array using the Heap Sort algorithm. This algorithm is an in-place comparison-based sort that uses a Binary Heap data structure.ReadHide
Problem Description
Given an array of integers, sort the array using the Heap Sort algorithm. This algorithm is an in-place comparison-based sort that uses a Binary Heap data structure.
Code it here
Tab indents · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Build a Max-Heap from the input data. Repeatedly swap the root (maximum element) with the last element of the heap, reduce the heap size, and heapify the new root.
Approaches
1. Max-Heap Implementation (Optimal) - O(N log N) ⭐
Idea: This algorithm consists of two main phases:
- Build Max-Heap: Transform the input array into a Max-Heap in
O(N)time. - Sort: Repeatedly extract the maximum element (the root) and place it at the end of the array. After each extraction, use the
heapifyfunction to maintain the heap property inO(log N)time.
Key Advantages:
- In-place: Requires only
O(1)auxiliary space. - Guaranteed Performance: Unlike Quick Sort, it has a guaranteed
O(N log N)worst-case time complexity.
Solution code
from typing import List
class Solution:
def sortArray(self, nums: List[int]) -> List[int]:
n = len(nums)
# 1. Build a Max-Heap (O(N))
# Start from the last non-leaf node and move up to the root
for i in range(n // 2 - 1, -1, -1):
self._heapify(nums, n, i)
# 2. Extract elements one by one (O(N log N))
for i in range(n - 1, 0, -1):
# Swap the root (current max) with the last element
nums[i], nums[0] = nums[0], nums[i]
# Call heapify on the reduced heap
self._heapify(nums, i, 0)
return nums
def _heapify(self, arr: List[int], n: int, i: int):
largest = i
left = 2 * i + 1
right = 2 * i + 2
# If left child exists and is greater than current largest
if left < n and arr[left] > arr[largest]:
largest = left
# If right child exists and is greater than current largest
if right < n and arr[right] > arr[largest]:
largest = right
# If largest is not root
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i] # swap
# Recursively heapify the affected sub-tree
self._heapify(arr, n, largest)Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language