SLIDE 3 3
13
$#%''(
744!$% ,-!',-! /,- %)*+
- However,toimplementinsertItem(), wemustnowscanthroughthe
entiresequenceintheworstcase.ThusinsertItem() runsin
14
Anobservation… Withanunsortedsequence… removeMin()always takesO(n) Butwithasortedsequence… insertItem()takesatmost O(n)
15
##-'.
$ 4$# 8 . 4 ,!- 8 . $%4 /,-
16
AlgorithmPriorityQueueSort(S,P): Input:AsequenceSstoringnelements,onwhicha totalorderrelationisdefined,andaPriority QueuePthatcompareskeyswiththesamerelation Output:TheSequenceSsortedbythetotal
while!S.isEmpty() do e← S.removeFirst() P.insertItem(e,e) whilePisnotemptydo e← P.removeMin() S.insertLast(e)
17
## - ''
VariationofPriorityQueueSort thatusesanunsortedsequence to implementthepriorityqueueP. – Phase1,theinsertionofanitemintoPtakesO(1)time – Phase2,removinganitemfromPtakestimeproportionaltothe currentnumberofelementsinP
''),+
) 4*#
) / %
4*,2-