You are on page 1of 51

Sort-Heap sort

(Third Edition)
Outline
◗ Heaps
◗ Maintaining the heap
property
◗ Building a heap
◗ The heapsort algorithm
◗ Priority queues
3
4
5
6
7
8
9
10
◗ heapsort algorithm.
◗ with worst case running time O(nlgn)
◗ an in‐place sorting algorithm:
◗ thus, require at most O(1) additional memory

◗ the heap data structure.


◗ an useful data structure for heapsort
◗ makes an efficient priority queue
Heaps
◗ The (Binary) heap data structure is an array object that
can be viewed as a nearly complete binary tree /complete
binary tree.

2
16 3
14 10 1 2 3 4 5 6 7 8 9
4 5 7 10
6 16 14 10 8 7 9 3 2 4 1
8 3
7
8 9
9
2 4
1 10
Binary tree
representations
1 1

2 3 2 3

4 5 6 7 4 5 6 7

8 8 9 10
9 10 11 12 13 14 15

A complete binary tree of A nearly complete binary tree with


height 3. 10 nodes and height 3.
Attributes of a Heap
◗ An array A that presents a heap with two attributes:
◗ length[A]: the number of elements in the array.
◗ heap‐size[A]: the number of elements in the heap stored
with array A.
◗ length[A]  heap‐size[A]

2
16 3
14 10 1 2 3 4 5 6 7 8 9
10
4 5 7 16 14 10 8 7 9 3 2 4 1
8 6 3
7
8 9 length[A]= heapsize[A]=
9
2 4 10
1 10
Basic procedures
◗ If a complete binary tree with n nodes is represented
sequentially, then for any node with index i, 1  i  n, we
have
◗ A[1] is the root of the tree
◗ the parent PARENT(i) is at ⌊i/2⌋ if i  1
◗ the left child LEFT(i) is at 2i
◗ the right child RIGHT(i) is at 2i+1
2
16
1 3
14 10 1 2 3 4 5 6 7 8 9
10
4 5 7 16 14 10 8 7 9 3 2 4 1
8 6 3
7
8 9
9
2 4
1 10

Heap properties
There are two kind of binary heaps: max‐heaps and min‐
heaps.
◗ In a max‐heap, the max‐heap property is that for every node i
other than the root,
A[PARENT(i) ]  A[i] .
◗ the largest element in a max‐heap is stored at the root
◗ In a min‐heap, the min‐heap property is that for every node i
other than the root,
A[PARENT(i) ]  A[i] .
◗ the smallest element in a min‐heap is at the root
Max and min heaps
14 9 30

12 7 6 3 25

10 8 6 5

Max
Heaps

2 10 11

7 4 20 83 21

10 8 6 50

Min
Heaps
The height of a heap
◗ The height of a node in a heap is the number of edges on
the longest simple downward path from the node to a
leaf, and the height of the heap to be the height of the
root, that is
(lgn).
◗ For
◗ theexample:
height of node 2 is 2
1
◗ the height of the heap is
16
3 2 3
14 10
4 5 7
8 6 3
7
8 9
9
2 4 1
10
◗ The MAX‐HEAPIFY procedure, which runs in O(lgn) time, is the key to
maintaining the max‐heap property.
◗ The BUILD‐MAX‐HEAP procedure, which runs in O(n) time,
produces a max‐heap from an unordered input array.
◗ The HEAPSORT procedure, which runs in O(nlgn) time, sorts an array
in place.
The MAX‐HEAPIFY procedure /2

◗ MAX‐HEAPIFY is an important subroutine for manipulating


max heaps.
◗ Input: an array A and an index i
◗ Output: the subtree rooted at index i becomes a max heap
◗ Assume: the binary trees rooted at LEFT(i) and RIGHT(i) are
max‐heaps, but A[i] may be smaller than its children
◗ Method: let the value at A[i] “float down” in the max‐heap
The MAX‐ HEAPIFY procedure
22
23
24
25
26
27
28
29
30
31
32
An example
1

2 16 3

14 10

4 5 6 7

8 7 9 3

8 9 10
i
2 4 1

1 2 3 4 5 6 7 8 9
10
1 2 3 4 7 8 9 10 14 16
A
[1] [1] [1]
16 1 1
[2] 14 [3] [2] 14 [3] [2] [3]

[4] 109 3 [4] 109 109 3


8 [5]
[6] [7] 8 [5]
[6] 14 [6] [7]
2 7 4 1 2 7 4 2 4 16
16 3 [4] 8 [5]
[8] [9] [8] [9]
[10] Initial [8] [9] Exchange 7 [10] Discard
heap [10] Heap size = [7] Heap size =
10 9
Discard Sorted=[16] Sorted=[16]
Heap size = 8 Exchange Readjust
Sorted=[14,16] Heap size = 9 Heap size =
Sorted=[14,16] 9
[1] 1 Sorted=[16]

[2] [2] 8 [1] 1 [2] 8


[3] [1] 14 [3]
8 [3]
[4] 9 3 [4] 4 [5] 7 109 [5] 7 109 3
4 10 [5] 3 [4]
[6] [7] [6] 4 [6] [7]
7 2 14 16 2 1 16
[7]
2 14 16 [8] [9] [8] [9]
[10] [10]
[1] [1] [1]
10 9 8
[2] 8 [3] 9 [2] 8 [3] 3 [2] 7 [3] 3

1 3 [4] 4 [5] 7 1 2 [4] 4 [5] 2 1 9


[4] 4 [5] 7
[6] [7] [6] [7] [6] [7]
10 14 16 10 14 16
2 14 16
[8] [9] [8] [9]
[8] [9] [10]
Readjust [10] Heap size = 7 [10] Heap size = 6
Heap size = 8 Sorted=[10,14,16] Sorted=[9,10,14,16]
Sorted=[14,16]

Heap size = 3 Heap size = 4 Heap size = 5


Sorted=[4,7,8,9,10,14,16] Sorted=[7,8,9,10,14,16] Sorted=[8,9,10,14,16]

[1] 3 [1] 4
[1] 7

[2] 4 [3]
[2] 2 [3] [2] 2 [3]
4 8 9 1 8 1 3 [5]
[4] 1[5] [4] 3 [5] 9 [4] 8 9
7 [6] 7 [6] 2
10 14 16 10 14 16 10 14 16 [6]
[7] [7] [7]
[8] [9] [8] [9] [8] [9]
[10] [10] [10]
36
37
Analysis

38
39
40
41
42
43
44
45
46
47
48
49
50
51

You might also like