Algo Chap 4 I Heap Sort
Algo Chap 4 I Heap Sort
• What is a “heap”?
• Definitions of heap:
A heap, in the context of data structure, is a
tree-based data structure that satisfies the
heap property.
A balanced, left-justified binary tree in which
no node has a value greater than the value in
its parent(max)
Left-justified binary trees
• Almost complete binary tree is a binary tree in which
every level, except possibly the last, is completely
filled, and all nodes are as far left as possible.
• A balanced binary tree is left-justified if:
– all the leaves are at the same depth, or
– all the leaves at depth n-1 are to the left of all the nodes at depth
n.
3
Contd.
2. The min heap can also be defined in the similar
manner as the tree in which each node of the
tree has a value that is at least as small as the
value of its children.
4
Contd.
2. In the case of max heap the root of the tree
contains the maximum value and in the case
of min heap the root of the tree contains the
minimum value if all the values are distinct.
3. The elements are filled from left to right on
the same level before moving to the next
level, therefore the heap is usually defined as
almost complete binary tree.
5
Heap
• Operations
– Creation of an empty heap
– Insertion of a new element into the heap
– Deletion of the largest(smallest) element from
the heap
6
Construction of heap
• A heap can be constructed by inserting new
elements one by one into an initially empty
heap into the left-most open spot in the array.
• If the newly inserted element is greater than
(in the case of max heap) its parent then swap
their position recursively so that the newly
inserted element rises to its appropriate place
and preserves the heap order.
7
The heap property
• A node has the heap property if the value in
the node is as large as or larger than the
values in its children
12 12 12
8 3 8 12 8 14
Blue node has Blue node has Blue node does not have
heap property heap property heap property
12 14
8 14 8 12
Blue node does not have Blue node has
heap property(max heap) heap property
12
Constructing a heap
8 8 10 10
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
Other children are not affected
12 12 14
10 5 14 5 12 5
8 14 8 10 8 10
exercise
• Let us take the list of
40,56,28,79,20,20,18,67 and 58 .
construct the max heap.
15
Contd.
• A max or min heap can be implemented using
an array where is X[i] is the element stored at
the root then its left child will be at X[2*i+1]
and right child will be at X[2*i+2].
• Here we must remember that the array index
starts from 0 to max-1
16
.
.
Heap sort
• Possible to use a heap to sort an array
• Place array items into a max heap
• Then remove them
– Items will be in descending order
– Place them back into the original array and they
will be in order
18
Algorithm(heap sort)
1. Build a heap from the input list
2. Repeatedly perform steps 3,4,5,&6 until
there remains only one item in the heap (or
until the heap is empty, possibly).
3. remove and save the root (max) of the heap
4. replace it with the last leaf (the heap is one
node smaller now)
5. “trickle down” the new root until the heap
property is restored.
6. return the max
3-Sorting-Intro-Heapsort 19
Example(Heap sort)
A trace of heapsort (a – c)
20
Heapsort
A trace of heapsort (d – f)
21
Heapsort
A trace of heapsort (g – i)
22
Heapsort
A trace of heapsort (j – l)
23
Application On Sorting: Heap Sort
– Array interpreted as a binary tree
1 2 3 4 5 6 7 8 9 10
26 5 77 1 61 11 59 15 48 19
[1] 26
input file
[2] 5 [3] 77
[2] 61 [3] 59
25
Heap Sort
• Exchange and adjust
[1] 77
26
Heap Sort
[1] 61
[2] 48 [3] 59
(b)
27
Heap Sort
[1] 48
[2] 19 [3] 26
[8] 59
59 [9] 61
61
[10] 77
[1] 26
(c)
[2] 19 [3] 11
48
[4] 15 [5] 5 [6] 1 [7] 48
59
[8] 59 [9] 61 [10] 77
(d)
28
Heap Sort
[1] 19
[2] 15 [3] 11
[4] 1
26
[5] 5 [6] 26 [7] 48
1
[8] 59
59 [9] 61
61
[10] 77
[1] 15
(e)
[2] 5 [3] 11
48
[4] 1
26
[5] 519 [6] 1 [7] 48
59
[8] 59 [9] 61 [10] 77
(f)
29
Heap Sort
[1] 11
[2] 5 [3] 1
15
[4] 1
19 26
[5] 5 [6] 26 [7] 48
1
[8] 59
59 [9] 61
61
[10] 77
[1] 5
(g)
[2] 1 [3] 111
48
[4] 115
26
[5] 519 [6] 1 [7] 48
59
[8] 59 [9] 61 [10] 77
(h)
30
Heap Sort
[1] 1
48
[4] 115
26
[5] 519 [6] 1 [7] 48
[8] 59
59 [9] 61 [10] 77
(i)
• So results
77 61 59 48 26 19 15 11 5 1
31