9
Most read
10
Most read
11
Most read
Data Structures and Algorithm
Prepared by
Mrs.P.Anusha M.Sc(IT).,M.Phil.,D.P.T.T.,(Ph.D).,
Assistant professor,
Department of Information Technology,
Bon secours college for women,
Thanjavur.
HEAP SORT
Heap Sort
• Heap - A max (min)heap is a complete binary tree with the
property that the value at each node is at least as large as (as
small as) the values at its children(if they exist). Call this
property the heap property.
Shape Property: Heap data structure is always a Complete
Binary Tree, which means all levels of the tree are fully filled.
Heap Property: All nodes are either greater than or equal
to or less than or equal to each of its children. If the parent
nodes are greater than their child nodes, heap is called a Max-
Heap, and if the parent nodes are smaller than their child
nodes, heap is called Min-Heap.
Heap sort
• In this sorting algorithm we first build a heap using given
elements.
• If we want to sort the elements in ascending order we create
a Min Heap.
• And to sort the elements in descending order we create a
Max Heap
• Once the heap is created we delete the root node from the
heap and put the last node in the root position and repeat
the steps till we have covered all elements.
Example: Sorting in Ascending Order
1. Build Heap
2. Transform the heap into min heap
3. Delete the root node
4. Put the last node of the heap in root position
5. Repeat from step 2 till all nodes are covered.
40 60 10 20 50 30
Input
• There are 6 elements. So our heap will have 6 nodes.
• We can represent the nodes of the heap in an array.
0 1 2 3 4 5 6
40 60 10 20 50 30
Array Index
Input
• Array Index starts from 0
• We can save the elements from index 1.
Build Heap
Create a Min Heap
Here 6 nodes
N = 6
Floor(N/2)
= floor(6/2)
= floor (3)
= 3
=> We start comparison from 3
• Min Heap the parent
node is always smaller
than or equal to its child
nodes.
• Is there any child node smaller than 60?
• Yes its 20
• So we swap position of 20 and 60
• Is there any child node smaller than 10?
No
• So we move to index 2 or the 2nd node.
• We move to index 1 of the 1st node
• Is there any child node smaller than 40?
• Yes, its 10
• So we swap 10 & 40
• Min Heap – a parent node is always smaller
than or equal to child nodes.
• Delete the root node
• Now we will take the element at the root
node and put it in our sorted element list
• So we have a min heap.
• Put the last node in the root position.
• Now there are 5 nodes. N = 5
• = floor (N/2)
• = floor (5/2)
• = 2.5
• = 2 (Start from 2 node)
0 1 2 3 4 5 6
10
Array Index
Sorted Element
• Any child node smaller than 20?
• No.
• So we move to index 1 or the 1st node
See, parent node are smaller than child node
So we have a min heap
0 1 2 3 4 5 6
10 20
Array Index
Sorted Element
• Is there any child node smaller than 40?
No
• So we move to index 1st node.
• Put the last node in the root position.
• Now there are 4 nodes. N = 4
• = floor (N/2)
• = floor (4/2)
• = 2 (Start from 2 node)
• Is there any child node smaller
than 50?
• Yes its 30
• So we swap position of 30 and 50
• Delete the root node
• Put last node in the root node
• 3 nodes N= 3
• = Floor (3/2)
• = Floor (1.5)
• = 1 (So we start from 1)
0 1 2 3 4 5 6
10 20 30
Array Index
Sorted Element
• Put last node parent position
• = floor (2/2)
• = floor (1)
• Child node smaller than 60?
• Yes its 40
• Swap 40 & 60
• Delete the parent node
• Sorted elements in ascending order.
0 1 2 3 4 5 6
10 20 30 40
Array Index
Sorted Element
0 1 2 3 4 5 6
10 20 30 40 50 60
Array Index
Sorted Element
Heap sort
Heap sort

More Related Content

PPT
Tree
PPTX
Working with arrays in php
PPTX
Arrays and linked lists
PPTX
Unit vii sorting
PDF
Trees second part in data structures with examples
PPT
PPTX
learn tree, linked list, queue, stack, and other algo
Tree
Working with arrays in php
Arrays and linked lists
Unit vii sorting
Trees second part in data structures with examples
learn tree, linked list, queue, stack, and other algo

Similar to Heap sort (20)

PPT
Lecture k-sorting
PPT
lecture-k-sorting.ppt
PPT
lecture to proide studeny with F-k-sorting.ppt
PDF
Insertion Sort Research and Algorithm.pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPT
Searching Sorting-SELECTION ,BUBBBLE.ppt
PPT
Advanced s and s algorithm.ppt
PPT
search_sort.ppt
PPTX
Analysis and Design of Algorithms -Sorting Algorithms and analysis
PPTX
CIRCULAR LINKED LIST _
PPT
Sorting algorithums > Data Structures & Algorithums
PPTX
Searching and sorting Techniques in Data structures
PPTX
Algorithm & data structures lec4&5
PPTX
Chapter 4.pptx
PPTX
Chapter 2. data structure and algorithm
PPTX
1.4 Sorting.pptx
PPTX
Lecture_Oct26.pptx
PPTX
Binary Search Tree
PPT
Heap tree
Lecture k-sorting
lecture-k-sorting.ppt
lecture to proide studeny with F-k-sorting.ppt
Insertion Sort Research and Algorithm.pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Searching Sorting-SELECTION ,BUBBBLE.ppt
Advanced s and s algorithm.ppt
search_sort.ppt
Analysis and Design of Algorithms -Sorting Algorithms and analysis
CIRCULAR LINKED LIST _
Sorting algorithums > Data Structures & Algorithums
Searching and sorting Techniques in Data structures
Algorithm & data structures lec4&5
Chapter 4.pptx
Chapter 2. data structure and algorithm
1.4 Sorting.pptx
Lecture_Oct26.pptx
Binary Search Tree
Heap tree
Ad

More from Anusha sivakumar (14)

PPTX
deadline.pptx
PPTX
Evolution of Computers ppt.pptx
PPTX
set operators.pptx
PPTX
PURPOSE OF DATABASE final.pptx
PPTX
NESTED SUBQUERY.pptx
PPTX
join relation.pptx
PPTX
fundamental relation algebra.pptx
PPTX
DBMS ARCHITECTURE.pptx
PPTX
Database user and administrator.pptx
PPTX
database language ppt.pptx
PPTX
basic structure of SQL FINAL.pptx
PPTX
AGGREGATE FUNCTION.pptx
PPTX
Greedy method
deadline.pptx
Evolution of Computers ppt.pptx
set operators.pptx
PURPOSE OF DATABASE final.pptx
NESTED SUBQUERY.pptx
join relation.pptx
fundamental relation algebra.pptx
DBMS ARCHITECTURE.pptx
Database user and administrator.pptx
database language ppt.pptx
basic structure of SQL FINAL.pptx
AGGREGATE FUNCTION.pptx
Greedy method
Ad

Recently uploaded (20)

PDF
AI-driven educational solutions for real-life interventions in the Philippine...
PPTX
What’s under the hood: Parsing standardized learning content for AI
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
Module on health assessment of CHN. pptx
PDF
M.Tech in Aerospace Engineering | BIT Mesra
PDF
plant tissues class 6-7 mcqs chatgpt.pdf
PDF
Climate and Adaptation MCQs class 7 from chatgpt
PDF
1.Salivary gland disease.pdf 3.Bleeding and Clotting Disorders.pdf important
PDF
Compact First Student's Book Cambridge Official
PPTX
Macbeth play - analysis .pptx english lit
PDF
Journal of Dental Science - UDMY (2022).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PPTX
INSTRUMENT AND INSTRUMENTATION PRESENTATION
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
Farming Based Livelihood Systems English Notes
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
Laparoscopic Colorectal Surgery at WLH Hospital
PDF
International_Financial_Reporting_Standa.pdf
AI-driven educational solutions for real-life interventions in the Philippine...
What’s under the hood: Parsing standardized learning content for AI
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
Module on health assessment of CHN. pptx
M.Tech in Aerospace Engineering | BIT Mesra
plant tissues class 6-7 mcqs chatgpt.pdf
Climate and Adaptation MCQs class 7 from chatgpt
1.Salivary gland disease.pdf 3.Bleeding and Clotting Disorders.pdf important
Compact First Student's Book Cambridge Official
Macbeth play - analysis .pptx english lit
Journal of Dental Science - UDMY (2022).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
INSTRUMENT AND INSTRUMENTATION PRESENTATION
Core Concepts of Personalized Learning and Virtual Learning Environments
Farming Based Livelihood Systems English Notes
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
Laparoscopic Colorectal Surgery at WLH Hospital
International_Financial_Reporting_Standa.pdf

Heap sort

  • 2. Prepared by Mrs.P.Anusha M.Sc(IT).,M.Phil.,D.P.T.T.,(Ph.D)., Assistant professor, Department of Information Technology, Bon secours college for women, Thanjavur.
  • 4. Heap Sort • Heap - A max (min)heap is a complete binary tree with the property that the value at each node is at least as large as (as small as) the values at its children(if they exist). Call this property the heap property. Shape Property: Heap data structure is always a Complete Binary Tree, which means all levels of the tree are fully filled. Heap Property: All nodes are either greater than or equal to or less than or equal to each of its children. If the parent nodes are greater than their child nodes, heap is called a Max- Heap, and if the parent nodes are smaller than their child nodes, heap is called Min-Heap.
  • 6. • In this sorting algorithm we first build a heap using given elements. • If we want to sort the elements in ascending order we create a Min Heap. • And to sort the elements in descending order we create a Max Heap
  • 7. • Once the heap is created we delete the root node from the heap and put the last node in the root position and repeat the steps till we have covered all elements. Example: Sorting in Ascending Order 1. Build Heap 2. Transform the heap into min heap 3. Delete the root node 4. Put the last node of the heap in root position 5. Repeat from step 2 till all nodes are covered.
  • 8. 40 60 10 20 50 30 Input • There are 6 elements. So our heap will have 6 nodes. • We can represent the nodes of the heap in an array. 0 1 2 3 4 5 6 40 60 10 20 50 30 Array Index Input • Array Index starts from 0 • We can save the elements from index 1.
  • 9. Build Heap Create a Min Heap Here 6 nodes N = 6 Floor(N/2) = floor(6/2) = floor (3) = 3 => We start comparison from 3 • Min Heap the parent node is always smaller than or equal to its child nodes.
  • 10. • Is there any child node smaller than 60? • Yes its 20 • So we swap position of 20 and 60 • Is there any child node smaller than 10? No • So we move to index 2 or the 2nd node. • We move to index 1 of the 1st node • Is there any child node smaller than 40? • Yes, its 10 • So we swap 10 & 40 • Min Heap – a parent node is always smaller than or equal to child nodes.
  • 11. • Delete the root node • Now we will take the element at the root node and put it in our sorted element list • So we have a min heap. • Put the last node in the root position. • Now there are 5 nodes. N = 5 • = floor (N/2) • = floor (5/2) • = 2.5 • = 2 (Start from 2 node) 0 1 2 3 4 5 6 10 Array Index Sorted Element
  • 12. • Any child node smaller than 20? • No. • So we move to index 1 or the 1st node See, parent node are smaller than child node So we have a min heap 0 1 2 3 4 5 6 10 20 Array Index Sorted Element
  • 13. • Is there any child node smaller than 40? No • So we move to index 1st node. • Put the last node in the root position. • Now there are 4 nodes. N = 4 • = floor (N/2) • = floor (4/2) • = 2 (Start from 2 node)
  • 14. • Is there any child node smaller than 50? • Yes its 30 • So we swap position of 30 and 50 • Delete the root node • Put last node in the root node • 3 nodes N= 3 • = Floor (3/2) • = Floor (1.5) • = 1 (So we start from 1) 0 1 2 3 4 5 6 10 20 30 Array Index Sorted Element
  • 15. • Put last node parent position • = floor (2/2) • = floor (1) • Child node smaller than 60? • Yes its 40 • Swap 40 & 60 • Delete the parent node • Sorted elements in ascending order. 0 1 2 3 4 5 6 10 20 30 40 Array Index Sorted Element 0 1 2 3 4 5 6 10 20 30 40 50 60 Array Index Sorted Element