SlideShare a Scribd company logo
Heap is a complete binary tree data
structure that satisfies the heap
property: for every node, the value
of its children is less than or equal
to its own value. Heaps are usually
used to implement priority queues,
where the smallest (or largest)
element is always at the root of the
tree
MAX HEAP
In a max heap, the parent
node's value is always
greater than or equal to the
values of its children. This
arrangement ensures that
the maximum element is at
the root of the heap
MIN HEAP
A min heap is a binary tree-based
data structure where the value of
each node is less than or equal to
the values of its children. In other
words, the smallest element is
always at the root. This satisfies
the min-heap property.
• The root node contains the
minimum value: 3.
• Each parent node has a
smaller value than its children.
• It satisfies the property of a min
heap.
A Priority queue is a BIFO container: The best one in
comes out first. That means that each element is
assigned a priority number, and the element with the
highest priority comes out first. Heaps are commonly
used to implement priority queues, where elements are
removed based on their priority. The highest (max heap)
or lowest (min heap) priority element is always at the
root.
Operation
on Heaps
Maintain/Restore the max-heap
property MAX-HEAPFY
Create a max-heap from an
unordered array BUILD-MAX-HEAP
Sort an array in place HEAPSORT
MAX-HEAPIFY OPERATION
Find location of largest value of A[i], A[left(i)], A[right(i)]
If not A[i], max-heap property does not hold.
Exchange A[i] with the larger of the two children to
preserve max-heap property.
Continue this process of compare/exchange down the
heap until sub-tree rooted at I is a max-heap.
At a leaf, the sub-tree rooted at the leaf is trivially a max-
heap.
Algorithm
MAX-HEAPIFY(A, I ,n)
{
l-=left(i)
R=right(i)
Largest=I
If l <=n and A[i] > A[largest]
Largest=I
If r <= n and A[r] > A[largest]
largest-=r
If largest!=I
Exchange(A[i] ,A[largest])
MAX-HEAPIFY(A, largest, n)
}
If any node violets the heap property then swap
this node with its larger children to maintain the
heap property, this is called heapify.
BUILD-MAX-HEAP
To build a max-heap from any tree, we can
thus start heapifying each sub-tree from the
bottom up and end up with a max-heap after
the function is applied on all elements
including the root element . In this case of
complete tree, the first index of non-leaf node
is given by n/2-1.
Algorithm
Build-max-Heap(A)
{
n=length[A]
For(i=floor(n/2);i>=1; i__)
{
MAX_HEAPIFY(A, I, n);
}
}
Heap sort is a comparison based
sorting technique based on Binary
Heap data structure. It is similar
to selection sort where we first find
the maximum element and place
the maximum elements at the end.
Build a max-heap from any array
Swap the root with the last element in the array
Discard this last node by decreasing the heap size
Perform Max-Heapify operation on the new root
node
Repeat this process until only one node remains.
g
Program for
implementation of Heap
sort
#include<stdio.h>
Void heapify(int arr[], int n, int i)
Int largest =I;
Int 1=2*i+1;
Int r=2*i+2;
If(l<n && arr[1] > arr[largest])
Largest=1;
If(r< && arr[r] >arr[largest])
Largest=r;
If (largest !=i)
[
Swap(arr[i], arr[largest]);
Heapify(arr, n, largest);
}}
Void heapsort(int arr[], int n)
{
For(int i=n/2-1; i>=0; i--)
Heapify(arr, n, i);
For(int i=n-1;i>=0; i--)
{
Swap(arr[0], arr[i]);
Heapify(arr, I, 0);
}}
Void printArray(int arr[], int n){
For(int i=0; i<n; ++i)
Cout <<arr[i] << “”;
Cout<< “/n”;
}
Int main()
{
Int arr[]={ 12, 11, 13, 5, 6, 7}
Int n=6;
Heapsort(arr n);
Printf(“Sorted array is n”);
printArray(arr,n);
}
OUTPUT
5 6 7 11 12 13
Heaps & its operation -Max Heap, Min  Heap

More Related Content

Similar to Heaps & its operation -Max Heap, Min Heap

4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
hasan Mohammad
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
AymericTaylor
 
Heap sort
Heap sortHeap sort
Heap sort
zahraa F.Muhsen
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Traian Rebedea
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
JinTaek Seo
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
Mariam Saeed
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
Israr63
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
Meghaj Mallick
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10Bianca Teşilă
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
hardmarcelia
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
manahilzulfiqar6
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
Bst Ali
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
Learning Courses Online
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
AmitShou
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
HemantSharma134028
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
ZainiXh
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
SudhaPatel11
 

Similar to Heaps & its operation -Max Heap, Min Heap (20)

4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heap tree
Heap treeHeap tree
Heap tree
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 

Recently uploaded

1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
TravisMalana
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
theahmadsaood
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
James Polillo
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 

Recently uploaded (20)

1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 

Heaps & its operation -Max Heap, Min Heap

  • 1.
  • 2. Heap is a complete binary tree data structure that satisfies the heap property: for every node, the value of its children is less than or equal to its own value. Heaps are usually used to implement priority queues, where the smallest (or largest) element is always at the root of the tree
  • 3. MAX HEAP In a max heap, the parent node's value is always greater than or equal to the values of its children. This arrangement ensures that the maximum element is at the root of the heap
  • 4. MIN HEAP A min heap is a binary tree-based data structure where the value of each node is less than or equal to the values of its children. In other words, the smallest element is always at the root. This satisfies the min-heap property. • The root node contains the minimum value: 3. • Each parent node has a smaller value than its children. • It satisfies the property of a min heap.
  • 5.
  • 6.
  • 7. A Priority queue is a BIFO container: The best one in comes out first. That means that each element is assigned a priority number, and the element with the highest priority comes out first. Heaps are commonly used to implement priority queues, where elements are removed based on their priority. The highest (max heap) or lowest (min heap) priority element is always at the root.
  • 8. Operation on Heaps Maintain/Restore the max-heap property MAX-HEAPFY Create a max-heap from an unordered array BUILD-MAX-HEAP Sort an array in place HEAPSORT
  • 9. MAX-HEAPIFY OPERATION Find location of largest value of A[i], A[left(i)], A[right(i)] If not A[i], max-heap property does not hold. Exchange A[i] with the larger of the two children to preserve max-heap property. Continue this process of compare/exchange down the heap until sub-tree rooted at I is a max-heap. At a leaf, the sub-tree rooted at the leaf is trivially a max- heap. Algorithm MAX-HEAPIFY(A, I ,n) { l-=left(i) R=right(i) Largest=I If l <=n and A[i] > A[largest] Largest=I If r <= n and A[r] > A[largest] largest-=r If largest!=I Exchange(A[i] ,A[largest]) MAX-HEAPIFY(A, largest, n) } If any node violets the heap property then swap this node with its larger children to maintain the heap property, this is called heapify.
  • 10.
  • 11. BUILD-MAX-HEAP To build a max-heap from any tree, we can thus start heapifying each sub-tree from the bottom up and end up with a max-heap after the function is applied on all elements including the root element . In this case of complete tree, the first index of non-leaf node is given by n/2-1. Algorithm Build-max-Heap(A) { n=length[A] For(i=floor(n/2);i>=1; i__) { MAX_HEAPIFY(A, I, n); } }
  • 12. Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum elements at the end. Build a max-heap from any array Swap the root with the last element in the array Discard this last node by decreasing the heap size Perform Max-Heapify operation on the new root node Repeat this process until only one node remains.
  • 13. g Program for implementation of Heap sort #include<stdio.h> Void heapify(int arr[], int n, int i) Int largest =I; Int 1=2*i+1; Int r=2*i+2; If(l<n && arr[1] > arr[largest]) Largest=1; If(r< && arr[r] >arr[largest]) Largest=r; If (largest !=i) [ Swap(arr[i], arr[largest]); Heapify(arr, n, largest); }} Void heapsort(int arr[], int n) { For(int i=n/2-1; i>=0; i--) Heapify(arr, n, i); For(int i=n-1;i>=0; i--) { Swap(arr[0], arr[i]); Heapify(arr, I, 0); }} Void printArray(int arr[], int n){ For(int i=0; i<n; ++i) Cout <<arr[i] << “”; Cout<< “/n”; } Int main() { Int arr[]={ 12, 11, 13, 5, 6, 7} Int n=6; Heapsort(arr n); Printf(“Sorted array is n”); printArray(arr,n); } OUTPUT 5 6 7 11 12 13