Question

Outline an algorithm for checking whether an array H[1..n) is a heap and determine its time efficiency.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ALGORITHM:

Considering max heap

We will run a loop for i= 1 to i<=n/2

We know that when a heap is represented in form of an array then considering the ith element to be the parent, it's left child is at index (2*i +1) and it's right child is at (2*i + 2)

So ,we will check if 2*i + 1 <=n then we will check if, H[2*i + 1] is less than H[i].If the second condition doesn't hold true then we will return false as our answer .

Doing the same for the right child.

After coming out of the for loop we will then return true.

Time Complexity:

The time complexity for this algorithm is O(n) because we are traversing the array once.

Add a comment
Know the answer?
Add Answer to:
Outline an algorithm for checking whether an array H[1..n) is a heap and determine its time...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT