SlideShare a Scribd company logo
1 of 41
Heap Sort
MSCS 1
Lecture 6
Advanced Analysis of Algorithm
Goals
• To explore the implementation, testing and performance of heap sort
algorithm.
Heap
A heap is a data structure that stores a collection of
objects (with keys), and has the following
properties:
• Complete Binary tree
• Heap Order
It is implemented as an array where each node in
the tree corresponds to an element of the array.
Heap
• The binary heap data structures is an array that can be viewed as
a complete binary tree. Each node of the binary tree corresponds
to an element of the array. The array is completely filled on all
levels except possibly lowest.
19
12 16
41 7
1619 1 412 7
Array A
Heap order property
• For every node v, other than the root, the key stored in v is greater or
equal (smaller or equal for max heap) than the key stored in the
parent of v.
• In this case the maximum value is stored in the root
Definition
• Max Heap
• Store data in ascending order
• Has property of
A[Parent(i)] ≥ A[i]
• Min Heap
• Store data in descending order
• Has property of
A[Parent(i)] ≤ A[i]
Max Heap Example
1619 1 412 7
Array A
19
12 16
41 7
Min heap example
127 191641
Array A
1
4 16
127 19
Insertion
• Algorithm
1. Add the new element to the next available position at the lowest
level
2. Restore the max-heap property if violated
• General strategy is percolate up (or bubble up): if
the parent of the element is smaller than the
element, then interchange the parent and child.
• OR
Restore the min-heap property if violated
• General strategy is percolate up (or bubble up): if
the parent of the element is larger than the
element, then interchange the parent and child.
19
12 16
41 7
19
12 16
41 7 17
19
12 17
41 7 16
Insert 17
swap
Percolate up to maintain the heap
property
Procedures on Heap
• Heapify
• Build Heap
• Heap Sort
Heap Sort
•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
element at the end. We repeat the same
process for remaining element.
What is Binary Heap?
• Let us first define a Complete Binary Tree. A complete binary tree is a
binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.
• A Binary Heap is a Complete Binary Tree where items are stored in a
special order such that value in a parent node is greater(or smaller)
than the values in its two children nodes. The former is called as max
heap and the latter is called min heap. The heap can be represented
by binary tree or array.
maxHeapify
•maxHeapify picks the largest child key and
compare it to the parent key. If parent key
is larger, than heapify quits, otherwise it
swaps the parent key with the largest child
key. So that the parent is now becomes
larger than its children.
Example of maxheapify
• Suppose we have a complete binary tree somewhere whose sub-
trees are heaps.
maxheapify
•
Example of maxheapify
Example of maxheapify
BUILD maxHEAP
• We can use the procedure 'Heapify' in a bottom-up fashion to
convert an array A[1 . . n] into a heap. Since the elements in the
subarray are all leaves, the procedure BUILD_HEAP goes through the
remaining nodes of the tree and runs ‘maxHeapify' on each one. The
bottom-up order of processing node guarantees that the subtree
rooted at children are maxheap before ‘maxHeapify' is run at their
parent.
Example: Convert the following array to a heap
16 4 7 1 12 19
Picture the array as a complete binary tree:
16
4 7
121 19
A =
16
4 7
121 19
16
4 19
121 7
16
12 19
41 7
19
12 16
41 7
swap
swap
swap
Heap Sort Algorithm
• The heap sort algorithm starts by using procedure BUILD-HEAP to
build a heap on the input array A[1 . . n]. Since the maximum
element of the array stored at the root A[1], it can be put into its
correct final position by exchanging it with A[n] (the last element in
A). If we now discard node n from the heap than the remaining
elements can be made into heap. Note that the new element at the
root may violate the heap property. All that is needed to restore the
heap property.
Heap Sort
• The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
• To sort the elements in the decreasing order, use a min heap
• To sort the elements in the increasing order, use a max heap
19
12 16
41 7
Example of Heap Sort
19
12 16
41 7
1912 16 1 4 7
Array A
Sorted:
Take out biggest
Move the last element
to the root
12 16
41
7
1912 16 1 47
Array A
Sorted:
maxHEAPIFY()
swap
12
16
41
7
191216 1 47
Array A
Sorted:
12
16
41
7
1912 161 47
Array A
Sorted:
Take out biggest
Move the last element
to the root
12
4
1
7
1912 1614 7
Array A
Sorted:
12
4
1
7
1912 1614 7
Array A
Sorted:
maxHEAPIFY()
swap
12
4
1
7
1912 1614 7
Array A
Sorted:
12
4
1
7
1912 1614 7
Array A
Sorted:
Take out biggest
Move the last
element to the
root
4
1
7
1912 161 4 7
Array A
Sorted:
swap
maxHEAPIFY()
4 1
7
1912 16147
Array A
Sorted:
4 1
7
1912 161 4 7
Array A
Sorted:
Move the last
element to the
root
Take out biggest
4
1
1912 1614 7
Array A
Sorted:
maxHEAPIFY()
swap
4
1
1912 161 4 7
Array A
Sorted:
Move the last
element to the
root
Take out biggest
1
1912 161 4 7
Array A
Sorted:
Take out biggest
1912 161 4 7
Sorted:
Time Analysis
• Build Heap Algorithm will run in O(n) time
• There are n-1 calls to Heapify each call requires O(log n) time
• Heap sort program combine Build Heap program and Heapify,
therefore it has the running time of O(n log n) time
• Total time complexity: O(n log n)
• Note: Building the max-heap from the unsorted list requires O(n)
calls to the maxheapify function, each of which takes O(logn) time.
Thus, the running time of build_heap is O(nlogn);
Possible Application
•Interval scheduling, when we have a lists of
certain task with start and finish times and we
want to do as many tasks as possible
•Sorting a list of elements that needs and efficient
sorting algorithm
Conclusion
• The primary advantage of the heap sort is its efficiency. The
execution time efficiency of the heap sort is O(n log n). The memory
efficiency of the heap sort, unlike the other n log n sorts, is constant,
O(1), because the heap sort algorithm is not recursive.
• The heap sort algorithm has two major steps. The first major step
involves transforming the complete tree into a heap. The second
major step is to perform the actual sort by extracting the largest
element from the root and transforming the remaining tree into a
heap.

More Related Content

What's hot

Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 
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
 
Hash table
Hash tableHash table
Hash tableVu Tran
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked ListSayantan Sur
 

What's hot (20)

Heap tree
Heap treeHeap tree
Heap tree
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
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
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Hash table
Hash tableHash table
Hash table
 
Heap tree
Heap treeHeap tree
Heap tree
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Heap sort
Heap sortHeap sort
Heap sort
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 
Stack
StackStack
Stack
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
stack presentation
stack presentationstack presentation
stack presentation
 
Searching
SearchingSearching
Searching
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Trees
TreesTrees
Trees
 
Linked list
Linked listLinked list
Linked list
 
Expression trees
Expression treesExpression trees
Expression trees
 

Similar to Heap Sort || Heapify Method || Build Max Heap Algorithm

Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptAmitShou
 
Heap Sort (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).pptSudhaPatel11
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsssuser7319f8
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptxZainiXh
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeksJinTaek Seo
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
Data structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxData structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxMalligaarjunanN
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
 

Similar to Heap Sort || Heapify Method || Build Max Heap Algorithm (20)

Heapsort
HeapsortHeapsort
Heapsort
 
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 (project).ppt
Heap Sort (project).pptHeap Sort (project).ppt
Heap Sort (project).ppt
 
heap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithmsheap sort in the design anad analysis of algorithms
heap sort in the design anad analysis of algorithms
 
Heap Sort 1053.pptx
Heap Sort 1053.pptxHeap Sort 1053.pptx
Heap Sort 1053.pptx
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
heaps2
heaps2heaps2
heaps2
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
05 heap 20161110_jintaeks
05 heap 20161110_jintaeks05 heap 20161110_jintaeks
05 heap 20161110_jintaeks
 
Ch15 Heap
Ch15 HeapCh15 Heap
Ch15 Heap
 
Heaps
HeapsHeaps
Heaps
 
4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Data structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxData structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptx
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
5-heap.ppt
5-heap.ppt5-heap.ppt
5-heap.ppt
 
5-heap.ppt
5-heap.ppt5-heap.ppt
5-heap.ppt
 

More from Learning Courses Online

More from Learning Courses Online (6)

Convolutional neural networks
Convolutional neural  networksConvolutional neural  networks
Convolutional neural networks
 
Introduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in AlgorithmIntroduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in Algorithm
 
Binary tree data structure
Binary tree data structureBinary tree data structure
Binary tree data structure
 
Fractional knapsack problem
Fractional knapsack problemFractional knapsack problem
Fractional knapsack problem
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Simple auto encoder
Simple auto encoderSimple auto encoder
Simple auto encoder
 

Recently uploaded

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Recently uploaded (20)

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

Heap Sort || Heapify Method || Build Max Heap Algorithm

  • 1. Heap Sort MSCS 1 Lecture 6 Advanced Analysis of Algorithm
  • 2. Goals • To explore the implementation, testing and performance of heap sort algorithm.
  • 3. Heap A heap is a data structure that stores a collection of objects (with keys), and has the following properties: • Complete Binary tree • Heap Order It is implemented as an array where each node in the tree corresponds to an element of the array.
  • 4. Heap • The binary heap data structures is an array that can be viewed as a complete binary tree. Each node of the binary tree corresponds to an element of the array. The array is completely filled on all levels except possibly lowest. 19 12 16 41 7 1619 1 412 7 Array A
  • 5. Heap order property • For every node v, other than the root, the key stored in v is greater or equal (smaller or equal for max heap) than the key stored in the parent of v. • In this case the maximum value is stored in the root
  • 6. Definition • Max Heap • Store data in ascending order • Has property of A[Parent(i)] ≥ A[i] • Min Heap • Store data in descending order • Has property of A[Parent(i)] ≤ A[i]
  • 7. Max Heap Example 1619 1 412 7 Array A 19 12 16 41 7
  • 8. Min heap example 127 191641 Array A 1 4 16 127 19
  • 9. Insertion • Algorithm 1. Add the new element to the next available position at the lowest level 2. Restore the max-heap property if violated • General strategy is percolate up (or bubble up): if the parent of the element is smaller than the element, then interchange the parent and child. • OR Restore the min-heap property if violated • General strategy is percolate up (or bubble up): if the parent of the element is larger than the element, then interchange the parent and child.
  • 10. 19 12 16 41 7 19 12 16 41 7 17 19 12 17 41 7 16 Insert 17 swap Percolate up to maintain the heap property
  • 11. Procedures on Heap • Heapify • Build Heap • Heap Sort
  • 12. Heap Sort •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 element at the end. We repeat the same process for remaining element.
  • 13. What is Binary Heap? • Let us first define a Complete Binary Tree. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. • A Binary Heap is a Complete Binary Tree where items are stored in a special order such that value in a parent node is greater(or smaller) than the values in its two children nodes. The former is called as max heap and the latter is called min heap. The heap can be represented by binary tree or array.
  • 14. maxHeapify •maxHeapify picks the largest child key and compare it to the parent key. If parent key is larger, than heapify quits, otherwise it swaps the parent key with the largest child key. So that the parent is now becomes larger than its children.
  • 15. Example of maxheapify • Suppose we have a complete binary tree somewhere whose sub- trees are heaps.
  • 19. BUILD maxHEAP • We can use the procedure 'Heapify' in a bottom-up fashion to convert an array A[1 . . n] into a heap. Since the elements in the subarray are all leaves, the procedure BUILD_HEAP goes through the remaining nodes of the tree and runs ‘maxHeapify' on each one. The bottom-up order of processing node guarantees that the subtree rooted at children are maxheap before ‘maxHeapify' is run at their parent.
  • 20. Example: Convert the following array to a heap 16 4 7 1 12 19 Picture the array as a complete binary tree: 16 4 7 121 19 A =
  • 21. 16 4 7 121 19 16 4 19 121 7 16 12 19 41 7 19 12 16 41 7 swap swap swap
  • 22. Heap Sort Algorithm • The heap sort algorithm starts by using procedure BUILD-HEAP to build a heap on the input array A[1 . . n]. Since the maximum element of the array stored at the root A[1], it can be put into its correct final position by exchanging it with A[n] (the last element in A). If we now discard node n from the heap than the remaining elements can be made into heap. Note that the new element at the root may violate the heap property. All that is needed to restore the heap property.
  • 23. Heap Sort • The heapsort algorithm consists of two phases: - build a heap from an arbitrary array - use the heap to sort the data • To sort the elements in the decreasing order, use a min heap • To sort the elements in the increasing order, use a max heap 19 12 16 41 7
  • 24. Example of Heap Sort 19 12 16 41 7 1912 16 1 4 7 Array A Sorted: Take out biggest Move the last element to the root
  • 25. 12 16 41 7 1912 16 1 47 Array A Sorted: maxHEAPIFY() swap
  • 27. 12 16 41 7 1912 161 47 Array A Sorted: Take out biggest Move the last element to the root
  • 29. 12 4 1 7 1912 1614 7 Array A Sorted: maxHEAPIFY() swap
  • 31. 12 4 1 7 1912 1614 7 Array A Sorted: Take out biggest Move the last element to the root
  • 32. 4 1 7 1912 161 4 7 Array A Sorted: swap maxHEAPIFY()
  • 34. 4 1 7 1912 161 4 7 Array A Sorted: Move the last element to the root Take out biggest
  • 35. 4 1 1912 1614 7 Array A Sorted: maxHEAPIFY() swap
  • 36. 4 1 1912 161 4 7 Array A Sorted: Move the last element to the root Take out biggest
  • 37. 1 1912 161 4 7 Array A Sorted: Take out biggest
  • 38. 1912 161 4 7 Sorted:
  • 39. Time Analysis • Build Heap Algorithm will run in O(n) time • There are n-1 calls to Heapify each call requires O(log n) time • Heap sort program combine Build Heap program and Heapify, therefore it has the running time of O(n log n) time • Total time complexity: O(n log n) • Note: Building the max-heap from the unsorted list requires O(n) calls to the maxheapify function, each of which takes O(logn) time. Thus, the running time of build_heap is O(nlogn);
  • 40. Possible Application •Interval scheduling, when we have a lists of certain task with start and finish times and we want to do as many tasks as possible •Sorting a list of elements that needs and efficient sorting algorithm
  • 41. Conclusion • The primary advantage of the heap sort is its efficiency. The execution time efficiency of the heap sort is O(n log n). The memory efficiency of the heap sort, unlike the other n log n sorts, is constant, O(1), because the heap sort algorithm is not recursive. • The heap sort algorithm has two major steps. The first major step involves transforming the complete tree into a heap. The second major step is to perform the actual sort by extracting the largest element from the root and transforming the remaining tree into a heap.