0% found this document useful (0 votes)
31 views31 pages

Algo Chap 4 I Heap Sort

Uploaded by

zerihun nana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views31 pages

Algo Chap 4 I Heap Sort

Uploaded by

zerihun nana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

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.

Left-justified Not left-justified


Contd.
• A heap is a special kind of a tree with the following
properties:
1. Each node of the tree has the value which is at
least as large as the value of its children. This type
of heap is called the max heap.

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

• All leaf nodes automatically have the heap


property
• A binary tree is a heap if all nodes in it have
Shift Up
• Given a node that does not have the heap property, you
can give it the heap property by exchanging its value with
the value of the larger child

12 14

8 14 8 12
Blue node does not have Blue node has
heap property(max heap) heap property

• This is sometimes called sifting up


• Notice that the child may have lost the heap property
Constructing a heap
• A tree consisting of a single node is automatically a heap
• We construct a heap by adding nodes one at a time:
– Add the node just to the right of the rightmost node in the
deepest level
– If the deepest level is full, start a new level
• Examples:

Add a new Add a new


node here node here
Constructing a heap
• Each time we add a node, we may destroy the heap
property of its parent node
• To fix this, we shift up
• But each time we shift up, the value of the topmost
node in the shift may increase, and this may destroy
the heap property of its parent node
• We repeat the shifting up process, moving up in the
tree, until either
– We reach nodes whose values don’t need to be swapped
(because the parent is still larger than both children), or
– We reach the root
Example
• Let us take the list of
8, 10, 5, 12 and 14 to construct the max
heap.

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

[4] 1 [5] 61 [6] 11 [7] 59

[8] 15 [9] 48 [10] 19


24
Heap Sort
• Adjust it to a MaxHeap
initial heap [1] 77

[2] 61 [3] 59

[4] 48 [5] 19 [6] 11 [7] 26

[8] 15 [9] 1 [10] 5

25
Heap Sort
• Exchange and adjust
[1] 77

[2] 61 [3] 59 exchange

[4] 48 [5] 19 [6] 11 [7] 26

[8] 15 [9] 1 [10] 5

26
Heap Sort
[1] 61

[2] 48 [3] 59

[4] 15 [5] 19 [6] 11 [7] 26

[8] 5 [9] 1 [10] 77 [1] 59

(a) [2] 48 [3] 26

[4] 15 [5] 19 [6] 11 [7] 1

[8] 5 [9] 61 [10] 77

(b)
27
Heap Sort
[1] 48

[2] 19 [3] 26

[4] 15 [5] 5 [6] 11 [7] 1

[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

[2] 15 [3] 111

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

You might also like