SlideShare une entreprise Scribd logo
1  sur  20
HEAPSORT
Name – Dinesh Kumar and Himanshu Sharma
Roll No. – 16032 and 16026 respectively
Submitted To : - Miss. Purnima Bindal
Heap: A special form of complete binary tree that key value of each node
is no smaller (larger) than the key value of its children (if any).
Max-Heap: Root node has the largest key.
A max tree is a tree in which the key value in
each node is no smaller than the key values in
its children. A max heap is a complete binary
tree that is also a max tree.
Min-Heap: root node has the smallest key.
A min tree is a tree in which the key value in
each node is no larger than the key values in
its children. A min heap is a complete binary
tree that is also a min tree.
Ex.
Lowest element
on top
Highest
element on top
61
61
Current Node
Visited node
swapping
Comparison between nodes
Abbrevations for diagrams
Current Node
Visited node
swapping
Comparison between nodes
 It is a well-known, traditional sorting algorithm
you will be expected to know.
 Heapsort is always O(n log n)
* Heapsort is better in time-critical
applications.
 A Binary Tree has the Heap Property iff,
* It is empty
* The key in the root is large than that in either
and both subtrees have the Heap property.
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
Conditions for creating a heap
input file
26
[1]
5[2]
77[3]
1
[4]
61
[5]
11
[6]
59
[7]
15
[8]
48
[9]
19
[10]
Function main
Input size of array n
Input array a[n]
Heapsort(a,n)
Function Heapsort (a [ ] , n)
t=n-1
for i = t to i >= 0
Build_Maxheap(a,i)
i = i - 1
swap(a,0,i)
Function swap (a [ ] , p , n)
temp=a[n]
a[n]=a[p]
a[p]=temp
Function max_heapify(a [ ] , i , n)
l=2*i+1
r=2*i+2
If l<=n && a[ l ]>a[ i ]
largest=l
else
largest=i
If r<=n && a[ r ] > a[ largest ]
largest=r
If largest ! = i
swap (a [ ] , i , largest)
max_heapify (a ,largest ,n )
Function Build_Maxheap(a [ ] , n)
For i=n/2 to i>=0
max_heapify(a ,i , n)
i = i - 1
26
[1]
5[2]
77[3]
1
[4]
61
[5]
11[6]
59
[7]
15
[8]
48
[9]
19
[10]
26
[1]
5[2]
77[3]
1
[4]
61
[5]
11
[6]
59
[7]
15
[8]
48
[9]
19
[10]
Function Build_Maxheap(a[],n)
For i=n/2 to i>=0
max_heapify(a ,i ,n)
i = i - 1
Starting with n/2
because it is our last
parent
Max_heapify function is
called
After heapifying the
tree we move to
next array element
i.e. its previous one
Parent = i
Left child
For parent = i = 4
Check a [ left ] > a [ parent ] if true
then largest = left
i.e. a [ 8 ] here
Now check if a [ right ] > a [ largest ] then largest =right
i.e. a [ 9 ] here means => 48 > 15,
So, swap ( parent , largest element among child )
i.e. swap(1 , 48)
26
[1]
5
[2]
77
[3]
48
[4]
61
[5]
11
[6]
59
[7]
15
[8]
1
[9]
19
[10]
26
[1]
5
[2]
77
[3]
48
[4]
61
[5]
11
[6]
59
[7]
15
[8]
1
[9]
19
[10]
26
[1]
5
[2]
77
[3]
48
[4]
61
[5]
11
[6]
59
[7]
15
[8]
1
[9]
19
[10]
26
[1]
61
[2]
77
[3]
48
[4]
5
[5]
11
[6]
59
[7]
15
[8]
1
[9]
19
[10]
For node =1
node.right <= array size above
condition not satisfied so move
further
26
[1]
61
[2]
77
[3]
48
[4]
19
[5]
11
[6]
59
[7]
15
[8]
1
[9]
5
[10]
26
[1]
61
[2]
77
[3]
48
[4]
19
[5]
11
[6]
59
[7]
15
[8]
1
[9]
5
[10]
77
[1]
61
[2]
26
[3]
48
[4]
19
[5]
11
[6]
59
[7]
15
[8]
1
[9]
5
[10]
77
[1]
61
[2]
59
[3]
48
[4]
19
[5]
11
[6]
26
[7]
15
[8]
1
[9]
5
[10]
• Adjust it to a MaxHeap
77[1]
61[2] 59[3]
48[4] 19[5] 11[6] 26[7]
15[8] 1[9] 5[10]
initial heap
• Exchange and adjust
77[1]
61[2] 59[3]
48[4] 19[5] 11[6] 26[7]
15[8] 1[9] 5[10]
exchange
61[1]
48[2] 59[3]
15[4] 19[5] 11[6]
26[7]
5[8] 1[9] 77[10]
59[1]
48[2] 26[3]
15[4] 19[5] 11[6]
1[7]
5[8] 61[9] 77[10]
(a)
(b)
Element Removed
from the tree
swap first element with last
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :>
Step 2 :>
Step 3 :>
Step 1 :> swap (a [0 ] , a[10])
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
48[1]
19[2] 26[3]
15[4] 5[5] 11[6]
1[7]
59[8] 61[9] 77[10] 26[1]
19[2] 11[3]
15[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(c)
(d)
59
6159
48
Step 1 :> swap (a [0 ] , a[last])
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
19[1]
15[2] 11[3]
1[4] 5[5] 26[6]
1[7]
59[8] 61[9] 77[10] 15[1]
5[2] 11[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(e)
(f)
59
6159
48
4826
2619
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
11[1]
5[2] 1[3]
1[4] 5[5] 26[6]
1[7]
59[8] 61[9] 77[10] 5[1]
1[2] 1[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(g)
(h)
59
6159
48
4826
2619
1915
15
11
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
swap first element with lastStep 1 :>
Step 2 :>
Step 3 :>
Decrease the tree size with 1
Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
• So finally our result is results
1[1]
1[2] 1[3]
1[4] 5[5] 1[6]
48[7]
59[8] 61[9] 77[10]
(i)
59
48261915
115
77 61 59 48 26 19 15 11 5 1
Step 2 :> Decrease the tree size with 1
Step 3 :> Max_heapify the whole tree
Step 1 :> swap (a [0 ] , a[last])
Analysis I
 Here’s how the algorithm starts:
heapify the array;
 Heapifying the array: we add each of n nodes
 Each node has to be sifted up, possibly as far as the root
 Since the binary tree is perfectly balanced, sifting up a single node
takes O(log n) time
 Since we do this n times, heapifying takes n*O(log n) time,
that is, O(n log n) time
Analysis II
 Here’s the rest of the algorithm:
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
 We do the while loop n times (actually, n-1 times), because
we remove one of the n nodes each time
 Removing and replacing the root takes O(1) time
 Therefore, the total time is n times however long it takes the
reheap method
Analysis III
 To reheap the root node, we have to follow one path from the root
to a leaf node (and we might stop before we reach a leaf)
 The binary tree is perfectly balanced
 Therefore, this path is O(log n) long
 And we only do O(1) operations at each node
 Therefore, reheaping takes O(log n) times
 Since we reheap inside a while loop that we do n times, the total
time for the while loop is n*O(log n), or O(n log n)
Analysis IV
 Here’s the algorithm again:
heapify the array;
while the array isn’t empty {
remove and replace the root;
reheap the new root node;
}
 We have seen that heapifying takes O(n log n) time
 The while loop takes O(n log n) time
 The total time is therefore O(n log n) + O(n log n)
 This is the same as O(n log n) time
Thank you
Name – Dinesh Kumar and Himanshu Sharma
Roll No. – 16032 and 16026 respectively
Submitted To : - Miss. Purnima Bindal

Contenu connexe

Tendances

lecture 4
lecture 4lecture 4
lecture 4sajinsc
 
Lab: Foundation of Concurrent and Distributed Systems
Lab: Foundation of Concurrent and Distributed SystemsLab: Foundation of Concurrent and Distributed Systems
Lab: Foundation of Concurrent and Distributed SystemsRuochun Tzeng
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Heaps
HeapsHeaps
HeapsIIUM
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsortAmit Rathi
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184Mahmoud Samir Fayed
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examplesBst Ali
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort Amit Kundu
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmssamairaakram
 

Tendances (20)

lecture 4
lecture 4lecture 4
lecture 4
 
Lab: Foundation of Concurrent and Distributed Systems
Lab: Foundation of Concurrent and Distributed SystemsLab: Foundation of Concurrent and Distributed Systems
Lab: Foundation of Concurrent and Distributed Systems
 
Heaps
HeapsHeaps
Heaps
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Heapsort using Heap
Heapsort using HeapHeapsort using Heap
Heapsort using Heap
 
Heaps
HeapsHeaps
Heaps
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Heaps
HeapsHeaps
Heaps
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Soft Heaps
Soft HeapsSoft Heaps
Soft Heaps
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Heap tree
Heap treeHeap tree
Heap tree
 
Presentation on Heap Sort
Presentation on Heap Sort Presentation on Heap Sort
Presentation on Heap Sort
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
Lec24
Lec24Lec24
Lec24
 

En vedette

Pecha kucha equipo 4
Pecha kucha  equipo 4Pecha kucha  equipo 4
Pecha kucha equipo 4veritoMU
 
INGRID NEW ACCOUNTS CV
INGRID NEW ACCOUNTS CVINGRID NEW ACCOUNTS CV
INGRID NEW ACCOUNTS CVIngrid Naidoo
 
The syntax tree data structure used in jbvd algorithm
The syntax tree data structure used in jbvd algorithmThe syntax tree data structure used in jbvd algorithm
The syntax tree data structure used in jbvd algorithmNicole Tryfona
 
இலக்கை அடைந்தே தீர்வது எப்படி
இலக்கை அடைந்தே தீர்வது எப்படிஇலக்கை அடைந்தே தீர்வது எப்படி
இலக்கை அடைந்தே தீர்வது எப்படிN Ganeshan
 
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016Agencia Nacional de Minería
 
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...Agencia Nacional de Minería
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnnTaiga Nomi
 
Residential housing company brochure beglinwoods architects
Residential housing company brochure beglinwoods architectsResidential housing company brochure beglinwoods architects
Residential housing company brochure beglinwoods architectsSimon Woods
 
Residential apartments company brochure beglinwoods architects
Residential apartments company brochure beglinwoods architectsResidential apartments company brochure beglinwoods architects
Residential apartments company brochure beglinwoods architectsSimon Woods
 

En vedette (12)

Pecha kucha equipo 4
Pecha kucha  equipo 4Pecha kucha  equipo 4
Pecha kucha equipo 4
 
INGRID NEW ACCOUNTS CV
INGRID NEW ACCOUNTS CVINGRID NEW ACCOUNTS CV
INGRID NEW ACCOUNTS CV
 
The Fight
The FightThe Fight
The Fight
 
The syntax tree data structure used in jbvd algorithm
The syntax tree data structure used in jbvd algorithmThe syntax tree data structure used in jbvd algorithm
The syntax tree data structure used in jbvd algorithm
 
இலக்கை அடைந்தே தீர்வது எப்படி
இலக்கை அடைந்தே தீர்வது எப்படிஇலக்கை அடைந்தே தீர்வது எப்படி
இலக்கை அடைந்தே தீர்வது எப்படி
 
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016
Estadística de emergencias mineras - acumulado 2010 2016 - corte 31.07.2016
 
The Royalton at Capitol Commons
The Royalton at Capitol CommonsThe Royalton at Capitol Commons
The Royalton at Capitol Commons
 
Voda čudo!
Voda čudo!Voda čudo!
Voda čudo!
 
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...
Estadísticas de seguimiento seguridad e higiene minera - 2015 - corte diciemb...
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnn
 
Residential housing company brochure beglinwoods architects
Residential housing company brochure beglinwoods architectsResidential housing company brochure beglinwoods architects
Residential housing company brochure beglinwoods architects
 
Residential apartments company brochure beglinwoods architects
Residential apartments company brochure beglinwoods architectsResidential apartments company brochure beglinwoods architects
Residential apartments company brochure beglinwoods architects
 

Similaire à heapsort_bydinesh

3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithmspppepito86
 
Lecture 3 complexity
Lecture 3 complexityLecture 3 complexity
Lecture 3 complexityMadhu Niket
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxdudelover
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptxabcdefgh690537
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structureShakil Ahmed
 

Similaire à heapsort_bydinesh (20)

3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
lecture 5
lecture 5lecture 5
lecture 5
 
lecture 6
lecture 6lecture 6
lecture 6
 
2.pptx
2.pptx2.pptx
2.pptx
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Lecture 3 complexity
Lecture 3 complexityLecture 3 complexity
Lecture 3 complexity
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Big O Notation
Big O NotationBig O Notation
Big O Notation
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
AA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptxAA_Unit 1_part-I.pptx
AA_Unit 1_part-I.pptx
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
UNIT I- Session 3.pptx
UNIT I- Session 3.pptxUNIT I- Session 3.pptx
UNIT I- Session 3.pptx
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 

Plus de Dinesh Kumar

CoupledsystemsbyDani
CoupledsystemsbyDaniCoupledsystemsbyDani
CoupledsystemsbyDaniDinesh Kumar
 
Input devices dani
Input devices daniInput devices dani
Input devices daniDinesh Kumar
 
Regulafalsi_bydinesh
Regulafalsi_bydineshRegulafalsi_bydinesh
Regulafalsi_bydineshDinesh Kumar
 
Database system utilities by dinesh
Database system utilities by dineshDatabase system utilities by dinesh
Database system utilities by dineshDinesh Kumar
 
Design engineering cohesion by dinesh
Design engineering cohesion by dineshDesign engineering cohesion by dinesh
Design engineering cohesion by dineshDinesh Kumar
 

Plus de Dinesh Kumar (6)

CoupledsystemsbyDani
CoupledsystemsbyDaniCoupledsystemsbyDani
CoupledsystemsbyDani
 
Input devices dani
Input devices daniInput devices dani
Input devices dani
 
independentevents
independenteventsindependentevents
independentevents
 
Regulafalsi_bydinesh
Regulafalsi_bydineshRegulafalsi_bydinesh
Regulafalsi_bydinesh
 
Database system utilities by dinesh
Database system utilities by dineshDatabase system utilities by dinesh
Database system utilities by dinesh
 
Design engineering cohesion by dinesh
Design engineering cohesion by dineshDesign engineering cohesion by dinesh
Design engineering cohesion by dinesh
 

Dernier

DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxAditiChauhan701637
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfTechSoup
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationMJDuyan
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 

Dernier (20)

Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
In - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptxIn - Vivo and In - Vitro Correlation.pptx
In - Vivo and In - Vitro Correlation.pptx
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Benefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive EducationBenefits & Challenges of Inclusive Education
Benefits & Challenges of Inclusive Education
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 

heapsort_bydinesh

  • 1. HEAPSORT Name – Dinesh Kumar and Himanshu Sharma Roll No. – 16032 and 16026 respectively Submitted To : - Miss. Purnima Bindal
  • 2. Heap: A special form of complete binary tree that key value of each node is no smaller (larger) than the key value of its children (if any). Max-Heap: Root node has the largest key. A max tree is a tree in which the key value in each node is no smaller than the key values in its children. A max heap is a complete binary tree that is also a max tree. Min-Heap: root node has the smallest key. A min tree is a tree in which the key value in each node is no larger than the key values in its children. A min heap is a complete binary tree that is also a min tree. Ex. Lowest element on top Highest element on top
  • 3. 61 61 Current Node Visited node swapping Comparison between nodes Abbrevations for diagrams Current Node Visited node swapping Comparison between nodes  It is a well-known, traditional sorting algorithm you will be expected to know.  Heapsort is always O(n log n) * Heapsort is better in time-critical applications.  A Binary Tree has the Heap Property iff, * It is empty * The key in the root is large than that in either and both subtrees have the Heap property.
  • 4. 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 Conditions for creating a heap input file 26 [1] 5[2] 77[3] 1 [4] 61 [5] 11 [6] 59 [7] 15 [8] 48 [9] 19 [10]
  • 5. Function main Input size of array n Input array a[n] Heapsort(a,n) Function Heapsort (a [ ] , n) t=n-1 for i = t to i >= 0 Build_Maxheap(a,i) i = i - 1 swap(a,0,i) Function swap (a [ ] , p , n) temp=a[n] a[n]=a[p] a[p]=temp Function max_heapify(a [ ] , i , n) l=2*i+1 r=2*i+2 If l<=n && a[ l ]>a[ i ] largest=l else largest=i If r<=n && a[ r ] > a[ largest ] largest=r If largest ! = i swap (a [ ] , i , largest) max_heapify (a ,largest ,n ) Function Build_Maxheap(a [ ] , n) For i=n/2 to i>=0 max_heapify(a ,i , n) i = i - 1
  • 6. 26 [1] 5[2] 77[3] 1 [4] 61 [5] 11[6] 59 [7] 15 [8] 48 [9] 19 [10] 26 [1] 5[2] 77[3] 1 [4] 61 [5] 11 [6] 59 [7] 15 [8] 48 [9] 19 [10] Function Build_Maxheap(a[],n) For i=n/2 to i>=0 max_heapify(a ,i ,n) i = i - 1 Starting with n/2 because it is our last parent Max_heapify function is called After heapifying the tree we move to next array element i.e. its previous one Parent = i Left child For parent = i = 4 Check a [ left ] > a [ parent ] if true then largest = left i.e. a [ 8 ] here Now check if a [ right ] > a [ largest ] then largest =right i.e. a [ 9 ] here means => 48 > 15, So, swap ( parent , largest element among child ) i.e. swap(1 , 48)
  • 9. • Adjust it to a MaxHeap 77[1] 61[2] 59[3] 48[4] 19[5] 11[6] 26[7] 15[8] 1[9] 5[10] initial heap
  • 10. • Exchange and adjust 77[1] 61[2] 59[3] 48[4] 19[5] 11[6] 26[7] 15[8] 1[9] 5[10] exchange
  • 11. 61[1] 48[2] 59[3] 15[4] 19[5] 11[6] 26[7] 5[8] 1[9] 77[10] 59[1] 48[2] 26[3] 15[4] 19[5] 11[6] 1[7] 5[8] 61[9] 77[10] (a) (b) Element Removed from the tree swap first element with last Decrease the tree size with 1 Max_heapify the whole tree Step 1 :> Step 2 :> Step 3 :> Step 1 :> swap (a [0 ] , a[10]) Step 2 :> Decrease the tree size with 1 Step 3 :> Max_heapify the whole tree
  • 12. 48[1] 19[2] 26[3] 15[4] 5[5] 11[6] 1[7] 59[8] 61[9] 77[10] 26[1] 19[2] 11[3] 15[4] 5[5] 1[6] 48[7] 59[8] 61[9] 77[10] (c) (d) 59 6159 48 Step 1 :> swap (a [0 ] , a[last]) Step 2 :> Decrease the tree size with 1 Step 3 :> Max_heapify the whole tree swap first element with lastStep 1 :> Step 2 :> Step 3 :> Decrease the tree size with 1 Max_heapify the whole tree
  • 13. 19[1] 15[2] 11[3] 1[4] 5[5] 26[6] 1[7] 59[8] 61[9] 77[10] 15[1] 5[2] 11[3] 1[4] 5[5] 1[6] 48[7] 59[8] 61[9] 77[10] (e) (f) 59 6159 48 4826 2619 Step 2 :> Decrease the tree size with 1 Step 3 :> Max_heapify the whole tree swap first element with lastStep 1 :> Step 2 :> Step 3 :> Decrease the tree size with 1 Max_heapify the whole tree Step 1 :> swap (a [0 ] , a[last])
  • 14. 11[1] 5[2] 1[3] 1[4] 5[5] 26[6] 1[7] 59[8] 61[9] 77[10] 5[1] 1[2] 1[3] 1[4] 5[5] 1[6] 48[7] 59[8] 61[9] 77[10] (g) (h) 59 6159 48 4826 2619 1915 15 11 Step 2 :> Decrease the tree size with 1 Step 3 :> Max_heapify the whole tree swap first element with lastStep 1 :> Step 2 :> Step 3 :> Decrease the tree size with 1 Max_heapify the whole tree Step 1 :> swap (a [0 ] , a[last])
  • 15. • So finally our result is results 1[1] 1[2] 1[3] 1[4] 5[5] 1[6] 48[7] 59[8] 61[9] 77[10] (i) 59 48261915 115 77 61 59 48 26 19 15 11 5 1 Step 2 :> Decrease the tree size with 1 Step 3 :> Max_heapify the whole tree Step 1 :> swap (a [0 ] , a[last])
  • 16. Analysis I  Here’s how the algorithm starts: heapify the array;  Heapifying the array: we add each of n nodes  Each node has to be sifted up, possibly as far as the root  Since the binary tree is perfectly balanced, sifting up a single node takes O(log n) time  Since we do this n times, heapifying takes n*O(log n) time, that is, O(n log n) time
  • 17. Analysis II  Here’s the rest of the algorithm: while the array isn’t empty { remove and replace the root; reheap the new root node; }  We do the while loop n times (actually, n-1 times), because we remove one of the n nodes each time  Removing and replacing the root takes O(1) time  Therefore, the total time is n times however long it takes the reheap method
  • 18. Analysis III  To reheap the root node, we have to follow one path from the root to a leaf node (and we might stop before we reach a leaf)  The binary tree is perfectly balanced  Therefore, this path is O(log n) long  And we only do O(1) operations at each node  Therefore, reheaping takes O(log n) times  Since we reheap inside a while loop that we do n times, the total time for the while loop is n*O(log n), or O(n log n)
  • 19. Analysis IV  Here’s the algorithm again: heapify the array; while the array isn’t empty { remove and replace the root; reheap the new root node; }  We have seen that heapifying takes O(n log n) time  The while loop takes O(n log n) time  The total time is therefore O(n log n) + O(n log n)  This is the same as O(n log n) time
  • 20. Thank you Name – Dinesh Kumar and Himanshu Sharma Roll No. – 16032 and 16026 respectively Submitted To : - Miss. Purnima Bindal