chapter6: heapsortbylander/cs5633/notes/heapsort.pdf · max-heapify parent left right figure...
TRANSCRIPT
CS 5633 Analysis of Algorithms Chapter 6: Slide – 1
Chapter 6: Heapsort
Max HeapsHeapsortPriority Queues
Parent, Left, Right
⊲ parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 2
Parent(i)return ⌊i/2⌋
Left(i)return 2i
Right(i)return 2i+ 1
Max-heap property: A[Parent(i)] ≥ A[i] forevery node i other than the root.
Figure 6.1
parent left right
⊲ figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 3
Max-Heapify
parent left right
figure 6.1
⊲ max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 4
Max-Heapify(A, i)l ← Left(i)r ← Right(i)if l ≤ A.heap-size and A[l] > A[i]then largest← lelse largest← i
if r ≤ A.heap-size and A[r] > A[largest]then largest← r
if largest 6= ithen exchange A[i]↔ A[largest]
Max-Heapify(A, largest)
Figure 6.2
parent left right
figure 6.1
max-heapify
⊲ figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 5
Heapsort
parent left right
figure 6.1
max-heapify
figure 6.2
⊲ heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 6
Build-Max-Heap(A)A.heap-size← A.length
for i← ⌊A.length/2⌋ downto 1Max-Heapify(A, i)
Heapsort(A)Build-Max-Heap(A)for i← A.length downto 2
exchange A[1]↔ A[i]A.heap-size← A.heap-size− 1Max-Heapify(A, 1)
Build-Max-Heap Behavior, Part 1
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
⊲ behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 7
# of Max-Heapify calls
1 ✲ 3(1) calls
���
❅❅❅
2 3 ✲ 2(2) calls
✁✁✁
❆❆❆
✁✁✁
❆❆❆
4 5 6 7 ✲ 0 calls
Total: 7 calls
Build-Max-Heap Behavior, Part 2
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
⊲ behavior 2
behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 8
# of Max-Heapify calls
1 ✲ 4(1) calls
���
❅❅❅
2 3 ✲ 3(2) calls
✁✁✁
❆❆❆
✁✁✁
❆❆❆
4 5 6 7 ✲ 2(4) calls
✄✄✄
✄✄✄
✄✄✄
✄✄✄
❈❈❈
❈❈❈
❈❈❈
❈❈❈
8 9 10 11 12 13 14 15 ✲ 0 calls
Total: 18 calls
Build-Max-Heap Behavior, Part 3
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
⊲ behavior 3
heap-extract-max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 9
Assuming n = 2k − 1 for k ≥ 1, the recurrence is:T (1) = 0T (n) = T (⌊n/2⌋) + ⌊n/2⌋+ ⌈n/4⌉
A simplification T (n) = T (n/2) + 3n/4 is Θ(n)by the Master Theorem.
Can show T (n) < 2n by mathematical induction.Basis n = 1: T (1) = 0 < 2Induction:Assuming T (n) < 2n, show T (2n+1) < 2(2n+1)
T (2n+ 1) = T (n) + n+ ⌈n/2⌉ < 2n+ n+ n =4n < 2(2n+ 1)
Heap-Extract-Max
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
⊲ heap-extract-
max
priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 10
Heap-Maximum(A)return A[1]
Heap-Extract-Max(A)if A.heap-size < 1then error “heap underflow”
max← A[1]A[1]← A[A.heap-size]A.heap-size← A.heap-size− 1Max-Heapify(A, 1)return max
Priority Queues
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
⊲ priority queues
set-key and insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 11
A max-priority queue supports the followingoperations:
� Insert(Q, x): Insert x into the Q.� Maximum(Q): Return the maximum of the
Q.� Extract-Max(Q): Remove the maximum
of the Q and return it.� Set-Key(Q, x, k): Set the key of x to k
(differs from book’s Increase-Key).
Set-Key and Insert
parent left right
figure 6.1
max-heapify
figure 6.2
heapsort
behavior 1
behavior 2
behavior 3
heap-extract-max
priority queues
⊲ set-key and
insert
CS 5633 Analysis of Algorithms Chapter 6: Slide – 12
Max-Heap-Set-Key(A, i, key)A[i] ← key
if A[Parent(i)] < key
then while i > 1 and A[Parent(i)] < key
exchange A[i]↔ A[Parent(i)]i← Parent(i)
else Max-Heapify(A, i)
Max-Heap-Insert(A, key)A.heap-size← A.heap-size+ 1Max-Heap-Set-Key(A,A.heap-size, key)