SlideShare a Scribd company logo
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
Data structures and algorithms lab10
PPT
Heap Sort (project).ppt
PPT
heap sort in the design anad analysis of algorithms
PPT
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
PPTX
heap Sort Algorithm
Tree
Working with arrays in php
Arrays and linked lists
Data structures and algorithms lab10
Heap Sort (project).ppt
heap sort in the design anad analysis of algorithms
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
heap Sort Algorithm

Similar to Heap sort (20)

PPT
Heapsort
PDF
Heap Hand note
PPT
Heap Sort (project).pptccccccccccccccccccccccccccccccccccc
PPT
Data Structure Heap Sort Algorithom PPT
PPT
Heap Sort (project).ppt
PPT
Heap Sort (project).ppt
PPTX
Heap sort
PPTX
Heap sort
PPTX
Algorithms - "heap sort"
PPTX
Heap_Sort1.pptx
PPTX
Heap Sort Algorithm
PPTX
Heap Sort in Design and Analysis of algorithms
PPTX
HEAP SORT .pptx
PPTX
Heaps and tries power point this is an educational material
PPTX
Heapsort using Heap
PPTX
05 heap 20161110_jintaeks
PPTX
Heap sort
PPT
Heap Sort || Heapify Method || Build Max Heap Algorithm
PPT
Chapter 10 ds
Heapsort
Heap Hand note
Heap Sort (project).pptccccccccccccccccccccccccccccccccccc
Data Structure Heap Sort Algorithom PPT
Heap Sort (project).ppt
Heap Sort (project).ppt
Heap sort
Heap sort
Algorithms - "heap sort"
Heap_Sort1.pptx
Heap Sort Algorithm
Heap Sort in Design and Analysis of algorithms
HEAP SORT .pptx
Heaps and tries power point this is an educational material
Heapsort using Heap
05 heap 20161110_jintaeks
Heap sort
Heap Sort || Heapify Method || Build Max Heap Algorithm
Chapter 10 ds
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
Business Ethics Teaching Materials for college
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharma ospi slides which help in ospi learning
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
master seminar digital applications in india
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Business Ethics Teaching Materials for college
102 student loan defaulters named and shamed – Is someone you know on the list?
Week 4 Term 3 Study Techniques revisited.pptx
Pre independence Education in Inndia.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Pharma ospi slides which help in ospi learning
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Final Presentation General Medicine 03-08-2024.pptx
master seminar digital applications in india
Module 4: Burden of Disease Tutorial Slides S2 2025
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Basic Mud Logging Guide for educational purpose
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES

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