1
1
- 2
- !
- "#
$ % &%
PriorityQueue
3
'( )'(
4
- Hidden
Implementation
SortedSequence UnsortedSequence
5
- '%!(
'%!(
- *$$$+
- %
6
- %$
*# % * ,-!$≤ ≤ ≤ ≤
:k≤ ≤ ≤ ≤ k :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k1,thenk1 k2 :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k3,thenk1≤ ≤ ≤ ≤ k3
1 Totalorderingexamples But arenottotalorderingssince - - PDF document
PriorityQueue
1
3
4
Implementation
SortedSequence UnsortedSequence
5
6
:k≤ ≤ ≤ ≤ k :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k1,thenk1 k2 :ifk1≤ ≤ ≤ ≤ k2andk2≤ ≤ ≤ ≤ k3,thenk1≤ ≤ ≤ ≤ k3
7
8
9
10
11
13
14
15
16
17
18
AlgorithmPriorityQueueSort(S,P): Input:AsequenceSstoringnelements,onwhicha totalorderrelationisdefined,andaPriority QueuePthatcompareskeyswiththesamerelation Output:TheSequenceSsortedbythetotal
while¬ ¬ ¬ ¬ S.isEmpty() do e← S.removeFirst() P.insertItem(e,e) while¬ ¬ ¬ ¬ P.isEmpty() do e← P.removeMin() S.insertLast(e)
19
20
21
'(S P Input: (7,4,8,2,5,3,9) () Phase1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (7,4) .. .. .. . . . (g) () (7,4,8,2,5,3,9) Phase2 (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) ()
22
– keepfirstpartofthe sequenceordered, selectmin,putitatits place ...exampleontheboard...
24
25
'(S (P Input: (7,4,8,2,5,3,9) () Phase1 (a) (4,8,2,5,3,9) (7) (b) (8,2,5,3,9) (4,7) (c) (2,5,3,9) (4,7,8) (d) (5,3,9) (2,4,7,8) (e) (3,9) (2,4,5,7,8) (f) (9) (2,3,4,5,7,8) (g) () (2,3,4,5,7,8,9) Phase2 (a) (2) (3,4,5,7,8,9) (b) (2,3) (4,5,7,8,9) .. .. .. . . . (g) (2,3,4,5,7,8,9) ()
26
– Wekeepsortedthe initialportionofthe sequence – Wecanuseswaps insteadofmodifying thesequence ...exampleontheboard...