Worst-case Complexity of Heap Sort (MCQ: Correct Option)

Worst-case Complexity of Heap Sort (MCQ: Correct Option)

Verified Sources
Sep 13, 2026

Heap sort has a worst-case (and also average-case) time complexity of O(nlog⁡n)O(n\log n) because it performs nn extraction steps, each involving a heap adjustment that costs O(log⁡n)O(\log n). Therefore, the correct answer is (iii) O(nlog⁡n)O(n\log n).

Key idea: after building a max-heap, each delete/extract-max requires “sift-down” (heapify-down) which traverses at most the height of the heap, i.e., O(log⁡n)O(\log n). Repeating this for nn elements yields O(nlog⁡n)O(n\log n).

Keywords: Heap sort Max-heap Heapify-down Time complexity

Heap Sort Complexity (Build Heap + Extract + Sift Down)

Why heap sort is O(nlog⁡n)O(n\log n) in the worst case

After the initial Build heap step, heap sort performs n−1n-1 extractions. Each extraction consists of:

  1. Swap the heap root with the last element.
  2. Restore heap order using heapify-down.

The heap height is O(log⁡n)O(\log n) for a binary heap, so the sift-down per extraction is O(log⁡n)O(\log n) even in the worst case (when the element moved must travel from root to a leaf).

Thus, worst-case time:

T(n)=O(n)+n⋅O(log⁡n)=O(nlog⁡n).T(n)=O(n)+n\cdot O(\log n)=O(n\log n).

Keywords: Binary heap Worst case Asymptotic analysis Root extraction

Heap sort runtime breakdown

  1. 1
    Step 1

    Convert the array into a valid heap (typically via bottom-up heapify). This costs O(n)O(n) in standard analysis.

  2. 2
    Step 2

    For k=nk=n down to 2: swap the root with the last unsorted element and reduce the heap size.

  3. 3
    Step 3

    Run heapify-down from the root; the element may move down at most the heap height, costing O(log⁡n)O(\log n).

  4. 4
    Step 4

    Total time is O(n)+(n−1)⋅O(log⁡n)=O(nlog⁡n)O(n) + (n-1)\cdot O(\log n)=O(n\log n), so the worst case is O(nlog⁡n)O(n\log n).

Pro Tip

When options include O(log⁡n)O(\log n) or O(n)O(n), remember heap sort must do nn extractions, and each extraction includes an O(log⁡n)O(\log n) heap adjustment.

Common confusion

The O(n)O(n) part comes from building the heap, but the dominating cost is the nn times heapify-down: n⋅O(log⁡n)=O(nlog⁡n)n\cdot O(\log n)=O(n\log n).

Heap sort worst-case runtime components

Dominating term determines the final asymptotic complexity.

Heap sort lifecycle (high-level)

Heap Construction

Step A

Bottom-up heapify makes the array a heap."

Repeated Extraction

Step B

Swap max/min at the root to its final position."

Sift Down (Re-heapify)

Step C

Restore heap property; each sift-down takes O(log⁡n)O(\log n). "

MCQ Answer & Rationale

Knowledge Check

Question 1 of 3
Q1Single choice

What is the worst-case time complexity of heap sort?