Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
http://www.aw-bc.com/info/levitin
Some examples and slides are based on lecture notes
created by Dr. Ben Choi, Louisiana Technical University
and Dr. Chuck Cusack, Hope College and Dr. Ellen
Walker from Hiram College
I have modified many of their slides and added new
slides.
Design and Analysis of Algorithms Chapter 6
Heapsort
Definition:
A heap is a binary tree with the following conditions:
it is essentially complete: (all levels are full except possibly the last level, where
only some rightmost leaves may be missing.)
9
5
4
7
2
tree 1
7
2
tree 2
9
5
7
2
tree 3
Design and Analysis of Algorithms Chapter 6
Definition implies:
Priority queues
Bottom-up construction
Example: 2 9 7 6 5 8 10
Efficiency:
Design and Analysis of Algorithms Chapter 6
Total:
h-1
i=0
2(h-i) 2i
= 2 ( n lg (n + 1)) = (n)
# nodes at level i
10
Example: 2 9 7 6 5 8 10
Efficiency:
11
12
13
In-class exercise
14
Heapsort Strategy
15
Heapsort Strategy
16
Heapsort Algorithm:
1.
Build heap
2.
3.
17
18
deleteMax()
in action
19
20
(log k)
k=n 1, n-2, 1
21
22
Representation
1 2 3 4 5 6
3
4
9 5 3 1 4 2
23
24
Heapsort example
25
26