SlideShare a Scribd company logo
Presentation On
Heap Sort
Presented by: ASGHAR ALI & TARIQ JAMIL.
Presented To: Prof. Anwar Rashad.
contents
• heap sort
• Binary Heap
• Properties of binary heap
• Heap sort definition
• Heapify Methods
• Heap sort Algorithm
Heap sort
To understand how to heap sort work.
First we need to understand some basic concept
related to binary heaps.
Binary heap
 Heap is a tree-based data structure in which all the tree nodes
are in a particular order .
 Such that the tree satisfies the heap properties
(that is a specific parent –child relationship is followed throughout
The tree ).
 A heap data structure where the tree is complete binary
tree is referred to as a binary heap .
 A binary tree in which :
 All level except the bottom-most level are completely filled .
 The last level may or may not be completely filled.
A complete binary tree
Properties of a binary heap
1) They are complete binary trees:
 This means all levels are totally filled (except maybe the last level),
And the node in the last level are as left as possible .
 This property makes array a suitable data structure for sorting
Binary heaps .
 We can easily calculate the indices of a nodes children ,so for parent
index k, the left child will be found at the index 2*i, and the right
child will be found at the index 2*i+1, (for indices that starts with 1).
 Similarly , for a child at the index i, its parents can be found at the index floor {(k)/2)}.
2) Heap are mainly of two types – max heap and min heap
• In max heap , the value of a node is(>=)always the value of each of the its children .
• In a min heap , the value of a parent is always (<=) the value of each of its children .
3.Root element
 In max heap , the elements at the root will always be maximum.
 In min heap, the root element will always be the smallest
 The heap sort algorithm takes advantages of this property
to sort an array using heaps .
Heap sort
 Heap sort is an efficient compression-based sorting algorithm
That:
 creates a heap from the input array.
 Than sorts the array taking advantages of the heap’s properties .
Heap method
 Before going into the working of heap sort, well visualized the array as a complete
binary tree. Next ,we turn it into a max heap using a process called heapification.
Cont…
 If all the sub trees in a binary tree are maxheaps themselves , the
whole tree is a maxheap.
 One way the idea implement this idea would be:
1. Start at the bottom of the tree.
2. Iterate through all the nodes are we travel to the top .
3. At each step , ensure that the node and all its children from a valid
max heap.
Heap Sort Algorithm
Step1:
Build a heap from the input data . Build a max heap to sort in increasing order and
build a main heap to sort in decreasing order.
Step2:
 Swap Root.
Swap the roots elements with the last item of the heap .
Step3:
 Reduce Heap Size :
Reduce the Heap size by one 1.
Step4:
 Re-heapify .
Step5:
 Call Recursively :
• Repeat steps 2,3,4 as long as the heap size is greater than 2.
1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7
HEAPIFIED ARRAY
1 2 3 4 5 6 7
23
10
15
54
77
12
34
Next
Next
77
54
10 15
23
12
34
23
10
15
54
12
77
34
23
54
10 15
77
12
34
UNSORTED ARRAY
1 2 3 4 5 6 7
23 10 12 54 15 34 77
23 10 12 54 15 34 77
77 54 23 10 15 34 12
23 54 77 10 15 34 12
23 10 77 54 15 34 12
NEXT
Next
Step 6. 1 2 3 4 5 6 7
77 54 23 10 15 34 12
12 54 23 10 15 34 77
54 12 23 10 15 34 77
34 12 23 10 15 54 77
15 12 23 10 34 54 77
23 12 15 10 34 54 77
10 12 15 23 34 54 77
15 12 10 23 34 54 77
10 12 15 23 34 54 77
Algorithm for heap sort
• Let ’A’ is a linear array .and ‘n’ is size of
array .
Heap sort (A ,n)
1) For k=n/2 to 1. [decrementation]
MaxHeapify(A,n,k); calling statement
[end of loop]
MaxHeapify(A,n,k)
2) Set largest = k, L=2*k, & R = (2*k)+1.
3) If L <= n & A[L] > A[largest] , then
set largest = L. [end of If Structure]
4) If R <= n & A[R] > A[largest] , then
set largest = R. [end of If Structure]
5) If largest ≠ k , then
a) interchange a[largest] , A[k]
b) MaxHeapify(A,n,k)
[end of If Structure]
Deleting heap()
6) For k = n to 1. [decrementation]
a) interchange A[1], A[k]
b) MaxHeapify(A,n,1)
[end of loop]
7) Exit.
HEAP SORT .pptx

More Related Content

What's hot

Network flows
Network flowsNetwork flows
Network flows
Richa Bandlas
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
Ridhima Chowdhury
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
sandeep54552
 
Stack
StackStack
ΠΛΗ30 ΜΑΘΗΜΑ 2.2
ΠΛΗ30 ΜΑΘΗΜΑ 2.2ΠΛΗ30 ΜΑΘΗΜΑ 2.2
ΠΛΗ30 ΜΑΘΗΜΑ 2.2
Dimitris Psounis
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Mrunal Patil
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
shrikant00786
 
Hashing
HashingHashing
Hashing
Ghaffar Khan
 
Heap tree
Heap treeHeap tree
Heap tree
JananiJ19
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
Mariam Saeed
 
Min priority queue
Min priority queueMin priority queue
Min priority queue
9854098540
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
Krish_ver2
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Vertex cover Problem
Vertex cover ProblemVertex cover Problem
Vertex cover Problem
Gajanand Sharma
 
Hash tables
Hash tablesHash tables
Hash tables
Rajendran
 
Greedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
Zeal Education Society, Pune
 
Heaps
HeapsHeaps

What's hot (20)

Network flows
Network flowsNetwork flows
Network flows
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Stack
StackStack
Stack
 
ΠΛΗ30 ΜΑΘΗΜΑ 2.2
ΠΛΗ30 ΜΑΘΗΜΑ 2.2ΠΛΗ30 ΜΑΘΗΜΑ 2.2
ΠΛΗ30 ΜΑΘΗΜΑ 2.2
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
Hashing
HashingHashing
Hashing
 
Heap tree
Heap treeHeap tree
Heap tree
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Min priority queue
Min priority queueMin priority queue
Min priority queue
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Vertex cover Problem
Vertex cover ProblemVertex cover Problem
Vertex cover Problem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Greedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. MohiteGreedy method by Dr. B. J. Mohite
Greedy method by Dr. B. J. Mohite
 
Heaps
HeapsHeaps
Heaps
 

Similar to HEAP SORT .pptx

Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
Meghaj Mallick
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
ZainiXh
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
ArjunSingh81957
 
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
samairaakram
 
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
 
Heapify algorithm
Heapify algorithmHeapify algorithm
Heapify algorithm
Sikandar Pandit
 
Heapsort
HeapsortHeapsort
Heapsort
Gopi Saiteja
 
Heapsort
HeapsortHeapsort
Heapsort
faribasavari
 
Heaptree
HeaptreeHeaptree
Heaptree
Rajapriya82
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
Israr63
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand note
Abdur Rouf
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
LegesseSamuel
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
hardmarcelia
 
Design and analysis of algorithm
Design and analysis of algorithmDesign and analysis of algorithm
Design and analysis of algorithm
laibaNoor60
 
Sorting algorithms in Data Structure
Sorting algorithms in Data StructureSorting algorithms in Data Structure
Sorting algorithms in Data Structure
Balamurugan M
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_Stacks
Hemanth Kumar
 
Binary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdfBinary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdf
aayutiwari2003
 
Binary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdfBinary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdf
aayutiwari2003
 
Maximum Subarray Sum in Python
Maximum Subarray Sum in PythonMaximum Subarray Sum in Python
Maximum Subarray Sum in Python
Kal Bartal
 
HeapSort
HeapSortHeapSort
HeapSort
Tuqa Rmahi
 

Similar to HEAP SORT .pptx (20)

Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
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
 
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
 
Heapify algorithm
Heapify algorithmHeapify algorithm
Heapify algorithm
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heapsort
HeapsortHeapsort
Heapsort
 
Heaptree
HeaptreeHeaptree
Heaptree
 
Lecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptxLecture 07 - HeapSort.pptx
Lecture 07 - HeapSort.pptx
 
Heap Hand note
Heap Hand noteHeap Hand note
Heap Hand note
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptxweek2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
 
Design and analysis of algorithm
Design and analysis of algorithmDesign and analysis of algorithm
Design and analysis of algorithm
 
Sorting algorithms in Data Structure
Sorting algorithms in Data StructureSorting algorithms in Data Structure
Sorting algorithms in Data Structure
 
ADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_StacksADS_Lec1_Linear_lists_Stacks
ADS_Lec1_Linear_lists_Stacks
 
Binary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdfBinary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdf
 
Binary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdfBinary heap in data structures algorithms.pdf
Binary heap in data structures algorithms.pdf
 
Maximum Subarray Sum in Python
Maximum Subarray Sum in PythonMaximum Subarray Sum in Python
Maximum Subarray Sum in Python
 
HeapSort
HeapSortHeapSort
HeapSort
 

Recently uploaded

Promt software engineer rEngineering.pdf
Promt software engineer rEngineering.pdfPromt software engineer rEngineering.pdf
Promt software engineer rEngineering.pdf
philipjuuu
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
ygdi1tl
 
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
ygdi1tl
 
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
ygdi1tl
 
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
ygdi1tl
 
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future withMUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
YvesTshefu1
 

Recently uploaded (6)

Promt software engineer rEngineering.pdf
Promt software engineer rEngineering.pdfPromt software engineer rEngineering.pdf
Promt software engineer rEngineering.pdf
 
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
一比一原版(dpu毕业证书)德保罗大学毕业证如何办理
 
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
一比一原版(uiuc毕业证书)伊利诺伊大学香槟分校毕业证如何办理
 
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
一比一原版(ui毕业证书)爱达荷大学毕业证如何办理
 
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future withMUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
MUWP SOLUTION by MUWPAY Bridging the current defi world to the future with
 

HEAP SORT .pptx

  • 1. Presentation On Heap Sort Presented by: ASGHAR ALI & TARIQ JAMIL. Presented To: Prof. Anwar Rashad.
  • 2. contents • heap sort • Binary Heap • Properties of binary heap • Heap sort definition • Heapify Methods • Heap sort Algorithm
  • 3. Heap sort To understand how to heap sort work. First we need to understand some basic concept related to binary heaps.
  • 4. Binary heap  Heap is a tree-based data structure in which all the tree nodes are in a particular order .  Such that the tree satisfies the heap properties (that is a specific parent –child relationship is followed throughout The tree ).  A heap data structure where the tree is complete binary tree is referred to as a binary heap .
  • 5.  A binary tree in which :  All level except the bottom-most level are completely filled .  The last level may or may not be completely filled. A complete binary tree
  • 6. Properties of a binary heap 1) They are complete binary trees:  This means all levels are totally filled (except maybe the last level), And the node in the last level are as left as possible .  This property makes array a suitable data structure for sorting Binary heaps .  We can easily calculate the indices of a nodes children ,so for parent index k, the left child will be found at the index 2*i, and the right child will be found at the index 2*i+1, (for indices that starts with 1).  Similarly , for a child at the index i, its parents can be found at the index floor {(k)/2)}.
  • 7. 2) Heap are mainly of two types – max heap and min heap • In max heap , the value of a node is(>=)always the value of each of the its children . • In a min heap , the value of a parent is always (<=) the value of each of its children .
  • 8. 3.Root element  In max heap , the elements at the root will always be maximum.  In min heap, the root element will always be the smallest  The heap sort algorithm takes advantages of this property to sort an array using heaps .
  • 9. Heap sort  Heap sort is an efficient compression-based sorting algorithm That:  creates a heap from the input array.  Than sorts the array taking advantages of the heap’s properties . Heap method  Before going into the working of heap sort, well visualized the array as a complete binary tree. Next ,we turn it into a max heap using a process called heapification.
  • 10. Cont…  If all the sub trees in a binary tree are maxheaps themselves , the whole tree is a maxheap.  One way the idea implement this idea would be: 1. Start at the bottom of the tree. 2. Iterate through all the nodes are we travel to the top . 3. At each step , ensure that the node and all its children from a valid max heap.
  • 11. Heap Sort Algorithm Step1: Build a heap from the input data . Build a max heap to sort in increasing order and build a main heap to sort in decreasing order. Step2:  Swap Root. Swap the roots elements with the last item of the heap . Step3:  Reduce Heap Size : Reduce the Heap size by one 1. Step4:  Re-heapify . Step5:  Call Recursively : • Repeat steps 2,3,4 as long as the heap size is greater than 2.
  • 12. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 HEAPIFIED ARRAY 1 2 3 4 5 6 7 23 10 15 54 77 12 34 Next Next 77 54 10 15 23 12 34 23 10 15 54 12 77 34 23 54 10 15 77 12 34 UNSORTED ARRAY 1 2 3 4 5 6 7 23 10 12 54 15 34 77 23 10 12 54 15 34 77 77 54 23 10 15 34 12 23 54 77 10 15 34 12 23 10 77 54 15 34 12 NEXT Next
  • 13. Step 6. 1 2 3 4 5 6 7 77 54 23 10 15 34 12 12 54 23 10 15 34 77 54 12 23 10 15 34 77 34 12 23 10 15 54 77 15 12 23 10 34 54 77 23 12 15 10 34 54 77 10 12 15 23 34 54 77 15 12 10 23 34 54 77 10 12 15 23 34 54 77
  • 14. Algorithm for heap sort • Let ’A’ is a linear array .and ‘n’ is size of array . Heap sort (A ,n) 1) For k=n/2 to 1. [decrementation] MaxHeapify(A,n,k); calling statement [end of loop] MaxHeapify(A,n,k) 2) Set largest = k, L=2*k, & R = (2*k)+1. 3) If L <= n & A[L] > A[largest] , then set largest = L. [end of If Structure] 4) If R <= n & A[R] > A[largest] , then set largest = R. [end of If Structure] 5) If largest ≠ k , then a) interchange a[largest] , A[k] b) MaxHeapify(A,n,k) [end of If Structure] Deleting heap() 6) For k = n to 1. [decrementation] a) interchange A[1], A[k] b) MaxHeapify(A,n,1) [end of loop] 7) Exit.