Download presentation
Presentation is loading. Please wait.
1
Lecture 5 HeapSort Priority queue and Heapsort
ACKNOWLEDGEMENTS: Some contents in this lecture source from COS226 of Princeton University by Kevin Wayne and Bob Sedgewick
2
Priority queues A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. A max-priority queue supports the following operations: insert(S, x) max(S) delete-max(S) decrease-key(x, key)
3
Array and sorted array Maximum Insert DeleteMax Array O(n) O(1)
Heap O(log n)
4
Data structure matters.
5
Almost complete binary tree
Array representation complete tree with N = 16 nodes (height = 4) Property. Height of an almost complete tree with N nodes is ⎣log N⎦.
6
Binary tree
7
Binary Heap A max-heap (min-heap) is an almost complete binary tree with vertices satisfying the max-heap (min-heap) property: If v and p(v) are a vertex and its parent respectively, then p(v) ≥ v (p(v) ≤ v).
8
Insert: SiftUp 16 14 10 8 7 9 3 2 18
9
Insert: SiftUp 16 14 10 8 7 9 3 2 18
10
Insert: SiftUp 16 14 10 18 7 9 3 2 8
11
Insert: SiftUp 16 18 10 14 7 9 3 2 8
12
Insert: SiftUp 18 16 10 14 7 9 3 2 8 O(logn)
13
Insert: SiftUp 16 14 10 SiftUp(H,i) 8 7 9 3 2 O(logn) 18
14
Maximum 16 14 10 8 7 9 3 2 4 Θ(1) return H[1];
15
Delete-maximum: SiftDown
16 O(2logn) 14 10 8 7 9 3 SiftDown(H,i) 2 4
16
Heap and Array Maximum Insert DeleteMax Array O(n) O(1) Sorted array
O(log n) d-heap O(logd n) O(dlogd n)
17
Heap sort Question: How to make a heap?
Create max-heap with all N keys. Repeatedly remove the maximum key. Question: How to make a heap?
18
How to make a heap
19
Heap sort
20
Quiz Give the array that results after heap construction on the following array of 10 keys: X U M V Y T A J I N Give the array that results after performing 3 successive delete-the-max on above constructed max heap. A. Y X T V M U A N I J B. Y X T V U M A J I N C. U N T J I M A D. U N T I J M A
21
Quiz What’s the time complexity of making a heap?
What’s the time complexity of heap sort? A. Θ(n) B. Θ(nlogn) C. Θ(n2) D. none of above
22
Applications Event-driven simulation. [customers in a line]
Data compression. [Huffman codes] Graph searching.[Dijkstra's algorithm, Prim's algorithm] Statistics. [maintain largest M values in a sequence] Operating systems.[load balancing, interrupt handling] Discrete optimization. [bin packing, scheduling] ......
23
Questions: Priority queue
Data structure = data representation + manipulation Explain what is Priority queue. Why not use array or sorted array to implement Priority queue? 9/21/2018 Xiaojuan
24
Questions: Heap Does this array represent a max-heap? Give the result array after following operations. (1) insert 15 (2) max (3) delete-max 9/21/2018 Xiaojuan
25
Questions: makeheap and heapsort
1. Give the array after heap construction on the following array of 10 keys: X U M V Y T A J I N 2. Is heapsort stable? In place? 9/21/2018 Xiaojuan
26
Questions: time complexity
Give the time complexity of following operations. Insert Max Delete-max Makeheap Heapsort 9/21/2018 Xiaojuan
27
Sort summary inplace? stable? worst average best remarks selection x
N exchanges insertion N 2 / 4 N use for small N or partially ordered shell ? tight code, subquadratic quick 2 N ln N N lg N N log N probabilistic guarantee fastest in practice 3-way quick improves quicksort in presence of duplicate keys merge N log N guarantee, stable heap 2 N lg N N log N guarantee, in-place ??? holy sorting grail
28
Complexity of Sort a < b yes no compares
height of tree = worst-case number of compares b < c yes no a < c yes no a b c a < c yes no b < c yes no b a c a c b c a b b c a c b a (at least) one leaf for each possible ordering
29
Complexity of Sort a < b yes no compares height of tree = worst-case number of compares b < c yes no a < c yes no b < c yes no a b c a < c yes no b a c Proposition. Any compare-based sorting algorithm must use at least log (n!) ~ nlogn compares in the worst-case. a c b c a b b c a c b a (at least) one leaf for each possible ordering
30
Sort summary inplace? stable? worst average best remarks selection x
N exchanges insertion N 2 / 4 N use for small N or partially ordered shell ? tight code, subquadratic quick 2 N ln N N lg N N log N probabilistic guarantee fastest in practice 3-way quick improves quicksort in presence of duplicate keys merge N log N guarantee, stable heap 2 N lg N N log N guarantee, in-place radix kN for numbers with k digits, or for string ??? holy sorting grail
31
Which sort algorithm?
32
Which sort algorithm?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.