Data Structures & AlgorithmsModerateTCS NQT · sorting

Problem: What is the number of comparisons needed by heapsort in the worst case for n elements?

  1. A. O(n log n)
  2. B. O(n)
  3. C. O(n^2)
  4. D. O(log n)

Correct answer: A. O(n log n)

Heapify is O(n) and each of the n extractions costs O(log n), so the total is O(n log n).

More Coding Interview Problems questions

Exams that ask this topic