Heapsort

Is Heapsort Just Heapify? How the Two Differ

No. Heapify fixes one node's place in a heap, while heapsort calls it repeatedly to build the heap and then to pull out values in sorted order.

On this page
  1. Short answer
  2. What heapify does
  3. What heapsort adds
  4. Heapify vs heapsort
  5. A quick check in Python
  6. Why people mix the two up

Short answer

No. Heapify is one operation: it restores heap order at a single node. Heapsort is a complete sorting algorithm that calls heapify many times, along with a loop that moves each value into its final place.

What heapify does

Heapify starts at one index and assumes the two subtrees below it are already heaps. The value at that index may still be smaller than one of its children. Heapify swaps the value with its larger child and keeps going down the tree until the value fits.

Here is a small example in a max-heap. The array [4, 10, 3] has a root of 4 with children 10 and 3. Since 10 is larger than 4, heapify swaps them, giving [10, 4, 3]. Now the root is at least as large as both of its children, so the heap order holds.

Insertion into a heap uses a related move called sift-up, which works upward rather than downward. Both are often grouped under the same idea of repairing heap order locally.

What heapsort adds

Heapsort uses heapify in two places:

  1. Build phase: call heapify on every non-leaf node, from the last parent back to the root. This turns the unordered array into a max-heap.
  2. Sort phase: repeatedly swap the root with the last item in the heap, shrink the heap by one, and heapify the new root. Each pass moves the largest remaining value into its final position.

So heapsort is a loop around heapify, plus the swaps. On its own, heapify does not sort anything. The full sequence is walked through in how the heapsort algorithm works.

Heapify vs heapsort

Heapify Heapsort
Input One node whose subtrees are already heaps An unsorted array
Output Heap order restored at that node A sorted array
Heapify calls One About n/2 in the build phase, then about n - 1 in the sort phase, roughly 1.5n in total
Time O(log n) for one call O(n log n) overall

A quick check in Python

Python’s heapq module has a heapify function. It turns a list into a min-heap in place, in linear time, but it does not sort the list:

import heapq

values = [4, 10, 3, 5, 1]
heapq.heapify(values)
print(values[0])  # 1, the smallest value, is at the root

To get sorted output from a heap, you pop items one at a time: [heapq.heappop(values) for _ in range(len(values))] returns [1, 3, 4, 5, 10]. That approach builds a separate output list, so it uses extra memory. Heapsort avoids this by placing each value inside the same array as it goes.

Why people mix the two up

Many explanations say “heapify” when they mean the whole method, and some use it for only the build phase. Once you see that heapify is the single operation, the two ideas separate cleanly. Heapify is the tool, and heapsort is the job the tool is used for.

The complete guide to heapsort

How the Heapsort Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →

AVL Trees in Data Structures

An AVL tree is a self-balancing binary search tree. Learn how balance factors and rotations keep search, insert, and delete at O(log n) in the worst case.

AVL Trees 4 min read