0% found this document useful (0 votes)
20 views55 pages

Heap Sort

The document provides a comprehensive overview of the Heap Sort algorithm, detailing the properties of heaps, including max-heaps and min-heaps, and their respective operations. It outlines the process of building a heap from an unordered array and the steps involved in sorting using the heap structure. Additionally, the document includes examples and illustrations to clarify the heapify process and the sorting algorithm.
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)
20 views55 pages

Heap Sort

The document provides a comprehensive overview of the Heap Sort algorithm, detailing the properties of heaps, including max-heaps and min-heaps, and their respective operations. It outlines the process of building a heap from an unordered array and the steps involved in sorting using the heap structure. Additionally, the document includes examples and illustrations to clarify the heapify process and the sorting algorithm.
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/ 55

Heap Sort Algorithm

SK. YAKOOB
Associate Professor
Definition of heap:
A heap is a data structure that stores a collection of
objects (with keys), and has the following properties:
1. 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
2. Heap Order:The ordering can be one of two types:
min-heap property: the value of each node is greater than or equal to
the value of its parent, with the minimum-value element at the root.
max-heap property: the value of each node is less than or equal to
the value of its parent, with the maximum-value element at the root.
Types of heap

Max
Heap
Min
Heap
1. Max heap :
Max-heap : is a complete binary tree in
which the value in each internal node is
greater than or equal to the values in the
children of that node.

Max-heap property:
 The key of a node is ≥ than the keys of its
children.
Max Heap Example
19

12 16

1 4 7

19 12 16 1 4 7

Array A
2. Min heap :
Min-heap : is a complete binary tree in
which the value in each internal node is
smaller than or equal to the values in the
children of that node.

Min-heap property:
 The key of a node is <= than the keys of its
children.
Min heap example
1

4 16

7 12 19

1 4 16 7 12 19

Array A
Max Heap
Max heap Operation
A heap can be stored as an
array A.
 Root of tree is A[1]
 Left child of A[i] = A[2i]
 Right child of A[i] = A[2i + 1]
 Parent of A[i] = A[ i/2 ]

8
Build Max-heap
Heapify() Example

1
6
4 1
0
1 7 9 3
4
2 8 1

A 16 4 10 14 7 9 3 2 8 1
=
Heapify() Example
1
6
4 1
0
1 7 9 3
4
2 8 1

A 16 4 10 14 7 9 3 2 8 1
=
Heapify() Example
1
6
4 1
0
1 7 9 3
4
2 8 1

A 16 4 10 14 7 9 3 2 8 1
=
Heapify() Example
1
6
1 1
4 0
4 7 9 3

2 8 1

A 16 14 10 4 7 9 3 2 8 1
=
Heapify() Example
1
6
1 1
4 0
4 7 9 3

2 8 1

A 16 14 10 4 7 9 3 2 8 1
=
Heapify() Example
1
6
1 1
4 0
4 7 9 3

2 8 1

A 16 14 10 4 7 9 3 2 8 1
=
Heapify() Example
1
6
1 1
4 0
8 7 9 3

2 4 1

A 16 14 10 8 7 9 3 2 4 1
=
Heapify() Example
1
6
1 1
4 0
8 7 9 3

2 4 1

A 16 14 10 8 7 9 3 2 4 1
=
Heapify() Example
1
6
1 1
4 0
8 7 9 3

2 4 1

A 1 1 1 8 7 9 3 2 4 1
= 6 4 0
Heapify() Example
1
6
1 1
4 0
8 7 9 3

2 4 1 20

A 1 1 1 8 7 9 3 2 4 1 20
= 6 4 0

Insert 20
Heapify() Example
1
6
1 1
4 0
8 2 9 3
0
2 4 1 7

A 1 1 1 8 20 9 3 2 4 1 7
= 6 4 0

Insert 20
Heapify() Example
1
6
20 1
0
8 1 9 3
4
2 4 1 7

A 1 2 1 8 14 9 3 2 4 1 20
= 6 0 0

Insert 20
Heapify() Example
20

16 1
0
8 1 9 3
4
2 4 1 7

A 2 1 1 8 14 9 3 2 4 1 7
= 0 6 0

Insert 20
2-Min heap :
min-heap Definition:
is a complete binary tree in which the
value in each internal node is lower
than or equal to the values in the
children of that node.

Min-heap property:
 The key of a node is <= than the
keys of its children.
Min heap Operation
A heap can be stored as an
array A.
 Root of tree is A[0]
 Left child of A[i] = A[2i+1]
 Right child of A[i] = A[2i + 2]
 Parent of A[i] = A[( i/2)-1]

3
Min Heap

19

12 16

1 4 7

19 12 16 1 4 7

Array A
Min Heap phase 1

19

1 16

12 4 7
Min Heap phase 1

19 7

12 4 16
Min Heap phase 1

4 7

12 19 16

1
,
Min Heap phase 2

16

4 7

12 19
Min Heap phase 2

16 7

12 19
Min Heap phase 2

12 7

16 19

1,
4
Min Heap phase 3

19

12 7

16
Min Heap phase 3

12 19

16

1,4
,7
Min Heap phase 4

16

12 19
Min Heap phase 4

12

16 19

1,4,7,
12
Min Heap phase 5

19

16
Min Heap phase 5

16

19

1,4,7,1
2,16
Min Heap phase 7

19

1,4,7,12,
16,19
Min heap final tree
1

4 7

12 16 19

1 4 7 12 1 6 19

Array A
Insertion
 Algorithm
1. Add the new element to the next available
position at the
lowest level
2. Restore the max-heap property if violated
 General strategy is percolate up (or
bubble up): if the parent of the element
is smaller than the element, then
interchange the parent and child.

OR

Restore the min-heap property if violated


 General strategy is percolate up (or bubble
up): if the parent of the element is larger
than the element, then interchange the
19 19

12 16 12 16

4 7 1 4 7 17
1

Insert 17
19

12 17
swap

1 4 7 16

Percolate up to maintain the heap


property
The heap sort algorithm has two
major steps :
The first major step involves transforming the
complete tree into a heap.

The second major step is to perform the actual sort


by extracting the largest or lowerst element from
the root and transforming the remaining tree into a
heap.
Heap-Sort
sorting strategy:
1. Build Max Heap from unordered array;
2. Find maximum element A[1];
3.Swap elements A[n] and A[1] :
now max element is at the end of the
array! .
4.Discard node n from heap
(by decrementing heap-size variable).
5.New root may violate max heap property, but
its children are max heaps. Run max_heapify
to fix this.
6.Go to Step 2 unless heap is empty.
HeapSort() Example
A = {16, 14, 10, 8, 7, 9,
3, 2, 4, 1}
1
2 3
16
1 1
4 4 5 6 0 7
8 7 9 3
8 9 1
2 4 0
1

20
HeapSort() Example
A = {14, 8, 10, 4, 7, 9, 3,
2, 1, 16}
1
2 3
14
8 1
4 5 6 0 7
4 7 9 3
8 9
2 1 16
i=
10

21
HeapSort() Example
A = {10, 8, 9, 4, 7, 1, 3,
2, 14, 16}
1
2 3
10
8 9
4 5 6 7
4 7 1 3
8
2 14 1
i=
6
9 1
0

22
HeapSort() Example
A = {9, 8, 3, 4, 7, 1, 2,
10, 14, 16}
1
2 3
9
8 3
4 5 6 7
4 7 1 2

10 1 1
i=
4 6
8 9 1
0

23
HeapSort() Example
A = {8, 7, 3, 4, 2, 1, 9,
10, 14, 16}
1
2 3
8
7 3
4 5 6
4 2 1 9
i=
1 1 1 7
0 4 6
8 9 1
0

24
HeapSort() Example
A = {7, 4, 3, 1, 2, 8, 9,
10, 14, 16}
1
2 3
7
4 3
4 5
1 2 8 9
i= 7
1 1 1 6
0 4 6
8 9 1
0

25
HeapSort() Example
A = {4, 2, 3, 1, 7, 8, 9,
10, 14, 16}
1
2 3
4
2 3
4 i=
5
1 8 9
7
6 7
1 1 1
0 4 6
8 9 1
0

26
HeapSort() Example
A = {3, 2, 1, 4, 7, 8, 9,
10, 14, 16}
1
2 3
3
2 1

i= 4 7 8 9
4
5 6 7
1 1 1
0 4 6
8 9 1
0

27
HeapSort() Example
A = {2, 1, 3, 4, 7, 8, 9,
10, 14, 16}
1
2
2 i=
3
1
3
4
4 7 8 9
5 6 7
1 1 1
0 4 6
8 9 1
0

28
HeapSort() Example
A = {1, 2, 3, 4, 7, 8, 9, 10, 14,
16} >>orederd
1
1
i 3
=2
3
2
4
4 7 8 9
5 6 7
1 1 1
0 4 6
8 9 1
0

29
Heap Sort pseudo code
void heapsort(int A[], int n)
{
int x,i,j;
x=A[n];
A[1]=A[n];
i=1;
j=2*i;
while(j<n-1)
{
if(A[j+1] > A[j])
j=j+1;
if(A[j+1]<A[j]
swap(A[i],A[j]);
i=j;
j=2*j;
else
break;
}
A[n]=x;
}
Conclusion
The primary advantage of the heap
sort is its efficiency. The execution
time efficiency of the heap sort is O(n
log n).

The memory efficiency of the heap sort,


unlike the other n log n sorts, is
constant, O(1), because the heap sort
algorithm is not recursive.

You might also like