insert(e): inserts an element eobject min(): returns (but does not remove) an element with smallest key; error if empty - return OremoveMin(): removes the element referenced by min(); error if empty - return Xinteger size()boolean empty()| Operation | Output | Priority Queue |
|---|---|---|
insert(5) |
- | {5} |
insert(9) |
- | {5, 9} |
insert(2) |
- | {2, 5, 9} |
insert(7) |
- | {2, 5, 7, 9} |
min() |
[2] | {2, 5, 7, 9} |
removeMin() |
- | {5, 7, 9} |
size() |
3 | {5, 7, 9} |
min() |
[5] | {5, 7, 9} |
removeMin() |
- | {7, 9} |
removeMin() |
- | {9} |
removeMin() |
- | {} |
empty() |
true |
{} |
removeMin() |
"error" |
{} |
min() returns a reference to an entry in the queue, not the actual value
min()과 removeMin()을 사용할 수 있기 때문에 Priority Queue는 정렬이 되어있을 필요는 없다.
insert()에서 탐색min()에서 탐색| Implementation with an unsorted list | Implementation with a sorted list |
|---|---|
![]() |
![]() |
| Performance | Performance |
insert takes O(1) time since we can insert the item at the beginning or end of the sequence |
insert takes O(n) time since we have to find the place where to insert the item |
removeMin and min take O(n) time since we have to traverse the entire sequence to find the smallest key |
removeMin and min take O(1) time, since the smallest key is at the beginning |

insert(e) operationsremoveMin() operationsAlgorithm PQ-Sort(L, P)
Input List L, priority queue P
Output Sorted list L
while not L.empty()
e ← L.front()
L.eraseFront()
P.insert(e)
while not P.empty()
e ← P.min()
P.removeMin()
L.insertBack(e)
Running time of Selection-sort:
O(n) timeremoveMin operations takes time proportional to

O(n^2) time

| List L | Priority Queue P (unsorted list) | |
|---|---|---|
| Input: | (7, 4, 8, 2, 5, 3, 9) | () |
| Phase 1 | ||
| (a) | (4, 8, 2, 5, 3, 9) | (7) |
| (b) | (8, 2, 5, 3, 9) | (7, 4) |
| … | … | .. |
| (g) | () | (7, 4, 8, 2, 5, 3, 9) |
| Phase 2 | ||
| (a) | (2) | (7, 4, 8, 5, 3, 9) |
| (b) | (2, 3) | (7, 4, 8, 5, 9) |
| (c) | (2, 3, 4) | (7, 8, 5, 9) |
| (d) | (2, 3, 4, 5) | (7, 8, 9) |
| (e) | (2, 3, 4, 5, 7) | (8, 9) |
| (f) | (2, 3, 4, 5, 7, 8) | (9) |
| (g) | (2, 3, 4, 5, 7, 8, 9) | () |
| List L | Priority Queue P (unsorted list) | |
|---|---|---|
| Input: | (22, 15, 36, 44, 10, 3, 9) | () |
| Phase 1 | ||
| (a) | (15, 36, 44, 10, 3, 9) | (22) |
| (b) | (36, 44, 10, 3, 9) | (22, 15) |
| … | … | .. |
| (g) | () | (22, 15, 36, 44, 10, 3, 9) |
| Phase 2 | ||
| (a) | (3) | (22, 15, 36, 44, 10, 9) |
| (b) | (3, 9) | (22, 15, 36, 44, 10) |
| (c) | (3, 9, 10) | (22, 15, 36, 44) |
| (d) | (3, 9, 10, 15) | (22, 36, 44) |
| (e) | (3, 9, 10, 15, 22) | (36, 44) |
| (f) | (3, 9, 10, 15, 22, 36) | (44) |
| (g) | (3, 9, 10, 15, 22, 36, 44) | () |
Running time of Insertion-sort:
n insert operations takes time proportional to

n removeMin operations takes O(n) timeO(n^2) time

| List L | Priority Queue P (sorted list) | |
|---|---|---|
| Input: | (7, 4, 8, 2, 5, 3, 9) | () |
| Phase 1 | ||
| (a) | (4, 8, 2, 5, 3, 9) | (7) |
| (b) | (8, 2, 5, 3, 9) | (4, 7) |
| … | … | .. |
| (g) | () | (2, 3, 4, 5, 7, 8, 9) |
| Phase 2 | ||
| (a) | (2) | (7, 4, 8, 5, 3, 9) |
| (b) | (2, 3) | (7, 4, 8, 5, 9) |
| (c) | (2, 3, 4) | (7, 8, 5, 9) |
| (d) | (2, 3, 4, 5) | (7, 8, 9) |
| (e) | (2, 3, 4, 5, 7) | (8, 9) |
| (f) | (2, 3, 4, 5, 7, 8) | (9) |
| (g) | (2, 3, 4, 5, 7, 8, 9) | () |
| List L | Priority Queue P (unsorted list) | |
|---|---|---|
| Input: | (22, 15, 36, 44, 10, 3, 9) | () |
| Phase 1 | ||
| (a) | (15, 36, 44, 10, 3, 9) | (22) |
| (b) | (36, 44, 10, 3, 9) | (15, 22) |
| … | … | .. |
| (g) | () | (3, 9, 10, 15, 22, 36, 44) |
| Phase 2 | ||
| (a) | (3) | (22, 15, 36, 44, 10, 9) |
| (b) | (3, 9) | (22, 15, 36, 44, 10) |
| (c) | (3, 9, 10) | (22, 15, 36, 44) |
| (d) | (3, 9, 10, 15) | (22, 36, 44) |
| (e) | (3, 9, 10, 15, 22) | (36, 44) |
| (f) | (3, 9, 10, 15, 22, 36) | (44) |
| (g) | (3, 9, 10, 15, 22, 36, 44) | () |
T is a binary tree storing keys at its nodes and satisfying the following properties:
v other than the root, key(v)>=key(parent(v))
Th be the height of the heap
T is completei = 0, ..., h-1,there are 2^i nodes at depth i
n keys has height O(logn)
h be the height of a heap storing n keys2^i keys at depth i = 0, ..., h-1 and at least one key at depth h,we have 
h has at most 2^h nodes: 


n keys by means of a vector of length n + 1i
2i2i+10 is not usedn+1removeMin corresponds to removing at rank n


Function insert of the priority queue ADT corresponds to the insertion of a key k to the heap
The insertion algorithm consists of three steps
z (the new last node)k at z
k, the heap-order property may be violatedk along an upward path from the insertion nodek reaches the root or a node whose parent has a key smaller than or equal to kO(logn), upheap runs in O(logn) time

removeMin of the priority queue ADT corresponds to the removal of the root key from the heapww

k of the last node, the heap-order property may be violatedk along a downward path from the rootk reaches a leaf or a node whose children have keys greater than or equal to kO(logn), down heap runs in O(logn) time


kk and with the two heaps as subtreesn items implemented by means of a heap
O(n)insert and removeMin take O(logn) timesize, empty, and min take time O(1) timen elements in O(nlogn) time



n given keys in using a bottom-up construction with log n phasesi, pairs of heaps with 2^i-1 keys are merged into heaps with 2^(i+1)-1 keys

O(n)O(n) timen successive insertions and speeds up the first phase of heap-sort