Priority
Priority
f00 pq 2
Priority Queues: Assumptions
f00 pq 3
Priority Queue Implementation
Unordered
O(1) O(n) O(n)
Array
f00 pq 4
Implementation with an Unsorted Sequence
•Performance summary
Operations Complexity
Insert() O(1)
deleteMin() O(n)
findMin() O(n)
6
Algorithm of priority queue using an unordered array
Algorithm enqueue(x)
if isfull() then throw FullQueueException
else
r r+1
Q[r] x
Algorithm dequeue()
if isEmpty() then throw EmptyQueueException
min f;
for i 1 to r do
if Q[min] > Q[i] then min i
item Q[min]
// replace the min with the last element
Q[min] Q[r];
r r - 1;
return item;
7
Implementation with Sorted Sequence
8
Algorithm of priority queue using an ordered
array
Algorithm enqueue(x)
if isfull() then throw FullQueueException
else
in-1
while (i >= 0 && x < Q[i]) do
Q[i + 1] Q[i]
i i-1
Q[i + 1] item
n n+1
Algorithm dequeue()
if isEmpty() then throw EmptyQueueException
else
item Q[f]
f f+1
return item
9
Heaps