UGC-NET Dec 2004 · Divide & Conquer: Mergesort

MCQ · 2 marks · Problem PROG-DIVC-0020

How much extra space is used by heapsort ?
  1. A
    $O (1)$
  2. B
    $O (\log n)$
  3. C
    $O (n)$
  4. D
    $O (n^2)$

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solutionHeapsort builds a max-heap inside the input array, then repeatedly swaps the root with the last element of the heap and sifts down. All of this is done by swaps within the array, and sift-down can be written iteratively.\[\text{Extra space}=O(1)\ \text{(a few index variables).}\]Heapsort is an in-place sorting algorithm, unlike merge sort, which needs \(O(n)\) auxiliary space.Answer: A

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com