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
 
Heap tree
Heap treeHeap tree
Heap tree
Shankar Bishnoi
 
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
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
ashish bansal
 
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 lab10
Bianca 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

Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
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
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
u86oixdj
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
sameer shah
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
nuttdpt
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
AndrzejJarynowski
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
Social Samosa
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
mbawufebxi
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
u86oixdj
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
Walaa Eldin Moustafa
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
eddie19851
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
mzpolocfi
 

Recently uploaded (20)

Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)Malana- Gimlet Market Analysis (Portfolio 2)
Malana- Gimlet Market Analysis (Portfolio 2)
 
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
原版制作(Deakin毕业证书)迪肯大学毕业证学位证一模一样
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
STATATHON: Unleashing the Power of Statistics in a 48-Hour Knowledge Extravag...
 
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
一比一原版(UCSF文凭证书)旧金山分校毕业证如何办理
 
Intelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicineIntelligence supported media monitoring in veterinary medicine
Intelligence supported media monitoring in veterinary medicine
 
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
4th Modern Marketing Reckoner by MMA Global India & Group M: 60+ experts on W...
 
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
一比一原版(Bradford毕业证书)布拉德福德大学毕业证如何办理
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
原版制作(swinburne毕业证书)斯威本科技大学毕业证毕业完成信一模一样
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data LakeViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
ViewShift: Hassle-free Dynamic Policy Enforcement for Every Data Lake
 
Nanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdfNanandann Nilekani's ppt On India's .pdf
Nanandann Nilekani's ppt On India's .pdf
 
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
一比一原版(Dalhousie毕业证书)达尔豪斯大学毕业证如何办理
 

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