SANJIVANI RURAL EDUCATION SOCIETY’S
SANJIVANI COLLEGE OF ENGINEERING, KOPARGAON-423603
(AN AUTONOMOUS INSTITUTE AFFILIATED TO SAVITRIBAI PHULE PUNE UNIVERSITY)
CIA-03 Activity of
Advance Data Structure
Group 10
Roll no Name
Department : Computer
46 Gaikwad Abhishek S.
Class : [Link]
Division : A 47 Gaikwad Tushar R.
Guided By:[Link] 48 Gambhire Manoj J.
49 Game Saburi A.
50 Gawali Kaushal R.
Presentation on
Heap Sorting
CONTENT
Introduction of Heap
Min heap
Max heap
Working of heap
Example
Difference
Advantages
Disadvantages
Conclusion
Reference
Introduction of Heap
Heap sort is an efficient comparison-based sorting algorithm that:
Creates a heap from the input array.
Then sorts the array by taking advantage of a heap's properties.
The concept of heap sort is to eliminate the element one by one from the heap part of
the list And then insert them into the sorted part of the list
Heap sort is similar to the selection sort where we first find the minimum element and
place minimum element at beginning. Repeat the same process for remaining element.
Heap sort is an in-place algorithm
The time complexity of heap sort is O(nlogn).
A Heap is a special Tree-based data structure in which the tree is a complete binary tree.
Since a heap is a complete binary tree, a heap with N nodes has log N height.
It is useful to remove the highest or lowest priority element.
There are two types of Heaps in the data structure.
1) MIN HEAP 2) MAX HEAP
In a Min-Heap
the
key
present
at
the
root
node
In a Max-Heap the key present at the root node
must be less than or equal among the keys must be greater than or equal among the keys
present at all of its children. present at all of its children.
The same property must be recursively true for The same property must be recursively true for
all sub-trees in that Binary Tree. all sub-trees in that Binary Tree.
In a Min-Heap the minimum key element present In a Max-Heap the maximum key element
at the root. present at the root.
Below is the Binary Tree that satisfies all the Below is the Binary Tree that satisfies all the
property of Min Heap. property of Max Heap.
Heapify Method:
Heapify is the process of creating a heap data structure from a binary tree represented using an array. It
is used to create Min-Heap or Max-heap. Start from the last index of the non-leaf node whose index is
given by n/2 – 1. Heapify uses recursion.
How does Heapify work?
12
Array :- 12 11 13 5 6 7
11 13
5 6 7
MAX HEAP is created using heapify method.
13
11 12
5 6 7
• Time complexity to build heap using Heapify method is O(n).
Working of heap tree
In heap sort algorithm
-it must be ACBT
-Create min/max heap.
-swap the first value with last value of the list.
-Repeat above steps till all the elements in the array are sorted.
4 3 7 1 8 5
Array :-
8 4 7 1 3 5
8 8 7 8
7 8 5 7 8 5 7 8 4 5 7 8
4 5 7 8 3 4 5 7 8
Final Array
1 3 4 5 7 8
Difference
HEAP SORT BUBBLE SORT SELECTION SORT
Defination Heapsort is a popular The bubble sort repeatedly Selection sorting is a sorting
and efficient sorting algorithm. compares the adjacent elements algorithm where we select the
The concept of heap sort is to and swaps them if they are not in minimum element from the array
eliminate the elements one by one the correct order. and put that at its correct position.
from the heap part of the list, and
then insert them into the sorted
part of the list.
Approach It acts like the divide It is based on the subtract and Selection sort performs minimum
and conquer conquer number of swaps to sort the array.
technique. method.
Time O(n logn) O(n) O(n^2)
Complexities
Method It uses the selection It is based on the This sorting algorithm uses the
method. exchanging method. selection
method
Speed This method is faster. This method is slower. This method is faster.
Advantages
Efficiency – The time required to perform Heap sort increases logarithmically
while other algorithms may grow exponentially slower as the number of items
to sort increases. This sorting algorithm is very efficient.
Memory Usage – Memory usage is minimal because apart from what is
necessary to hold the initial list of items to be sorted, it needs no additional
memory space to work
Simplicity – It is simpler to understand than other equally efficient sorting
algorithms because it does not use advanced computer science concepts such as
recursion.
Disadvantages
Costly: Heap sort is costly.
Unstable: Heap sort is unstable. It might rearrange the relative order.
Efficient: Heap Sort are not very efficient when working with highly
complex data.
Conclusion
The heap sort works internally in the binary heap, which is a completely balanced
binary tree that either has the largest value or the smallest value at its node.
Thus, heapify process is carried out recursively to get the sorted array.
In Heap Sorting, the first step is the process In Heap Sorting, as the name suggests, the
first step is the process In Heap Sorting, as the name suggests, the first step is the
process In Heap Sorting, as the name suggests, the first step is the process previous
node from the heap.
References
Heap Sort (With Code in Python, C++, Java and C) ([Link])
Heap Sort Tutorials & Notes | Algorithms | HackerEarth
Heap Sort - GeeksforGeeks
Thank you