Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Chapter 6
Heap Sort
6 -- 1
1D Array
Memory
a
start
1-dimensional array x = [a, y, f, k]
x[1] = a; x[2] = y;
x[3] = f;
x[4] = k
6 -- 2
2D Arrays
5
8
17
10
8
1
0
11
8
6 -- 3
column 0
column 2
column 3
column 1
6 -- 4
row 0
row 1
row 2
Sorting Revisited
avg case
worst case
Insertion sort
n2
n2
Merge sort
nlog n nlog n
Next on the agenda: Heapsort
Combines advantages of both previous algorithms
6 -- 5
Heaps
6 -- 6
Heaps
A = 16 14 10 8
1 =
16
14
10
8
2
7
4
6 -- 7
Parent(i
Parent(i)
return i/2
Left(i
Left(i)
return 2*i
Right(i
Right(i)
return 2*i + 1
1
10
16 15
10
Level: 3
2
6 -- 8
6 -- 9
Heapify() Example
16
4
10
14
2
7
8
A = 16 4 10 14 7
6 -- 12
Heapify() Example
16
4
10
14
2
7
8
A = 16 4 10 14 7
6 -- 13
Heapify() Example
16
4
10
14
2
7
8
A = 16 4 10 14 7
6 -- 14
Heapify() Example
16
14
10
4
2
7
8
A = 16 14 10 4
6 -- 15
Heapify() Example
16
14
10
4
2
7
8
A = 16 14 10 4
6 -- 16
Heapify() Example
16
14
10
4
2
7
8
A = 16 14 10 4
6 -- 17
Heapify() Example
16
14
10
8
2
7
4
A = 16 14 10 8
6 -- 18
Heapify() Example
16
14
10
8
2
7
4
A = 16 14 10 8
6 -- 19
Heapify() Example
16
14
10
8
2
7
4
A = 16 14 10 8
6 -- 20
Heap Height
Definitions:
The height of a node in the tree = the number of
edges on the longest downward path to a leaf
6 -- 21
for k = 1
2h-1
for k = 0 (leaves level) 2h-0
6 -- 22
= 20
= 21
= 22
= 23
=1
=2
=4
=8
= 2h-1
= 2h
Heap Height
(A.5)
Therefore
2h n 2h+1 - 1
h lg n
&
lg(n+1) 1 h
lg(n+1) 1 h lg n
Analyzing Heapify()
6 -- 24
Analyzing Heapify()
Analyzing Heapify()
So we have
T(n) T(2n/3) + (1)
By case 2 of the Master Theorem,
T(n) = O(lg n)
Alternately, Heapify takes T(n) = (h)
h = height of heap = lg n
T(n) = (lg n)
6 -- 26
We know that
2h+1-1 = n
2h = (n + 1)/2
2.2h = n + 1
= n/2 + 1 = n/2
has at most
So:
6 -- 28
BuildHeap()
// given an unsorted array A, make A a heap
BuildHeap(A)
{
1. heapheap-size[
size[A] length[
length[A]
2. for i length[
length[A]/2
]/2 downto 1
3.
do Heapify(A,
Heapify(A, i)
i)
}
BuildHeap() Example
4
2
16
10
14
6 -- 30
10
BuildHeap() Example
4
2
i=5
16
10
10
14
6 -- 31
BuildHeap() Example
4
2
i=4
2
8
10
14
16
10
6 -- 32
BuildHeap() Example
4
2
i=3
14
8
10
16
10
6 -- 33
BuildHeap() Example
4
i=2
10
14
8
10
16
6 -- 34
BuildHeap() Example
i=1
4
2
16
10
14
10
6 -- 35
BuildHeap() Example
16
2
14
10
10
6 -- 36
Analyzing BuildHeap()
k=0 to k xk = x/(1-x)2
where x =1/2
1
xi =
if x < 1 //d iffe re n tia te
1 x
i=0
1
i x i 1 =
//m u ltip ly b y x
2
i =1
(1 x )
i =1
i =1
i xi =
(1 x )
//p lu g in x =
1
2
i
1/ 2
=
= 2
i
2
1/ 4
Heapsort
6 -- 40
Heapsort
Heapsort(A)
{
1. BuildBuild-Heap(A
Heap(A)
2. for i length[
length[A] downto 2
3.
do exchange A[1] A[i]
4.
heapheap-size[
size[A] heapheap-size[
size[A] - 1
5.
Heapify(A
Heapify(A, 1)
}
6 -- 41
HeapSort() Example
16
2
14
10
10
6 -- 42
HeapSort() Example
14
2
10
16
i = 10
6 -- 43
HeapSort() Example
10
2
14
i=9
16
10
6 -- 44
HeapSort() Example
9
2
10
14
16
i=8
10
6 -- 45
HeapSort() Example
8
2
10
14
16
10
9
i=7
6 -- 46
HeapSort() Example
7
2
10
14
16
10
i=6
6 -- 47
HeapSort() Example
4
2
i=5
10
14
16
10
6 -- 48
HeapSort() Example
3
2
i=4
10
14
16
10
6 -- 49
HeapSort() Example
i=3
1
4
10
14
16
10
6 -- 50
HeapSort() Example
i =2
2
4
10
14
16
10
6 -- 51
Analyzing Heapsort
6 -- 52
Analyzing Heapsort
6 -- 53
Max-Priority Queues
Applications:
scheduling jobs on a shared computer
prioritizing events to be processed based on their
predicted time of occurrence.
Printer queue
6 -- 54
Maximum(S
return A[1]
Maximum(S):
A[1]
returns the element of S with the largest key (value)
ExtractExtract-Max(S
Max(S):
removes and returns the element of S with the largest key
IncreaseIncrease-Key(S
Key(S, x, k):
k):
increases the value of element xs key to the new value k,
x.value k
Insert(S,
Insert(S, x):
x):
inserts the element x into the set S, i.e. S S {x}
6 -- 55
Extract-Max(A)
1.
2.
3.
4.
6 -- 56
Increase-Key(A, i, key)
// increase a value (key) in the array
1. if key < A[i]
2. then error new key is smaller than current key
key
3. A[i] key
4. while i > 1 and A[Parent(i
[Parent(i)] < A[i]
5.
do exchange A[i] A[Parent(i
[Parent(i)]
6.
i Parent(i
Parent(i) // move index up to parent
6 -- 57
Increase-Key() Example
16
2
14
10
i=9
10
6 -- 58
Increase-Key() Example
16
2
14
10
8
8
7
i=9
10
15
6 -- 59
Increase-Key() Example
16
2
14
10
i=4
15
8
7
9
10
6 -- 60
Increase-Key() Example
16
2
15
10
i=4
14
8
7
9
10
6 -- 61
Insert(A, key)
6 -- 62
6 -- 63
Running Time