• edited by
14,234 views
52 52 votes

An array $X$ of $n$ distinct integers is interpreted as a complete binary tree. The index of the first element of the array is $0$. If only the root node does not satisfy the heap property, the algorithm to convert the complete binary tree into a heap has the best asymptotic time complexity of

  1. $O (n)$
  2. $O (\log n)$
  3. $O (n \log n)$
  4. $O (n \log \log n)$

3 Answers

Best answer
51 51 votes
Here we need to call Heapify/ Bubble down/ Percolate down procedure on Root, which in the worst case will take time $O (\log n).$ So $B$ is the correct option.

Other options do not even make sense, because with $O(n)$ we can even build an entire Heap not just heapify on the root. $O (n \log n) \;\&\; O (n \log \log n)$ is more than $O(n)$.
• edited by
27 27 votes
The question is saying best case which will be when only one swap will be required which will be order of $1$.
As no option matches just call heapify at the root - $O(\log n)$.
• edited by
7 7 votes
Step 1:delete the root and replace it with last element and heapify takes O(log n) Step 2: inserting the deleted element again takes O(log n) time so its O(2log n) and finally O(log n)
Answer:
Position:
Show:

Related questions

25 25 votes
1 answers 1 answer
9.6k
9.6k views
Ishrat Jahan asked Oct 31, 2014
9,622 views
Which of the following sequences of array elements forms a heap?$\{23, 17, 14, 6, 13, 10, 1, 12, 7, 5\}$$\{23, 17, 14, 6, 13, 10, 1, 5, 7, 12\}$$\{23, 17, 14, 7, 13, 10, ...
48 48 votes
7 answers 7 answers
17.6k
17.6k views
Ishrat Jahan asked Oct 31, 2014
17,576 views
Let $L$ be a context-free language and $M$ a regular language. Then the language $L ∩ M$ isalways regularnever regularalways a deterministic context-free languagealways a...
40 40 votes
1 answers 1 answer
12.3k
12.3k views
Ishrat Jahan asked Oct 31, 2014
12,306 views
Which of the following statements about regular languages is NOT true ?Every language has a regular supersetEvery language has a regular subsetEvery subset of a regular l...
4 4 votes
2 answers 2 answers
4.5k
4.5k views
Ishrat Jahan asked Oct 31, 2014
4,477 views
The cyclomatic complexity of the flow graph of a program providesan upper bound for the number of tests that must be conducted to ensure that all statements have been exe...