# regarding min-PQ

• November 15th, 2012, 04:30 PM
IHeartProgramming
regarding min-PQ
Hi, I read heaps are efficient to build a min-PQ and in turn to use an array to make a min-heap. THe problem is if I print the list, it's not truly items in a min-PQ for example:

I could have a heap of: 3,6,7,15,16,8
so represented as a binary tree:
3
6 7
15 16 8

So as a heap, all node's are less than its children, but once I print the array: 3,6,7,15,16,8, It should be: 3,6,7,8,15,16. Why is this point never brought up whenever I heart efficiency of a heap to build a PQ (max or min PQ)? What should I do to ensure it's in ascending order, should heap sort be the best way
• November 15th, 2012, 05:01 PM
copeg
Re: regarding min-PQ
First, by PQ I presume you mean Priority Queue - for future reference you should be a lot more explicit when posting.

Quote:

So as a heap, all node's are less than its children, but once I print the array: 3,6,7,15,16,8, It should be: 3,6,7,8,15,16. Why is this point never brought up whenever I heart efficiency of a heap to build a PQ (max or min PQ)?
A heap satisfies the heap property - which defines the relationship between parent and child, there is nothing within this property that suggests sibling order. Hence represented within an array a heap may not be a sorted array. A heap as a priority queue is efficient given its log(n) complexity for inserts and deletes.

Quote:

What should I do to ensure it's in ascending order, should heap sort be the best way
What is the goal? If it is to sort an array, then sort the array (which begs the question why you want a priority queue implemented as a heap when your goal is to fully sort)