SlideShare a Scribd company logo
1 of 43
Presented By:
Introduction to Heapsort
• Running time: O(n lg n)
Like merge sort
• Sorts in place: only a constant number of array elements are stored
outside the input array at any time
Like insertion sort
• Heap
A data structure used by Heapsort to manage information during the
execution of the algorithm
• Can be used as an efficient priority queue
Perfect Binary Tree
• For binary tree with height h
All nodes at levels h–1 or less have 2 children (full)
Complete Binary Trees
• For binary tree with height h
All nodes at levels h–2 or less have 2 children (full)
All leaves on level h are as far left as possible
Complete Binary Trees
Heaps
• Two key properties
• Complete binary tree
• Value at node
• Smaller than or equal to values in sub-trees (min-heap)
• Greater than or equal to values in sub-trees (max-heap)
• Example max-heap
Y ≤ X
Z ≤ X
Heap and Non-heap Examples
Binary Heap
• An array object that can be viewed as a nearly complete binary tree
• Each tree node corresponds to an array element that stores the value in the
tree node
• The tree is completely filled on all levels except possibly the lowest, which is
filled from the left up to a point
• A has two attributes
• length[A]: number of elements in the array
• heap-size[A]: number of elements in the heap stored within A
heap-size[A] ≤ length[A]
• max-heap and min-heap
A Max-heap
Length and Heap-size
Basic procedures
• Given the index i of a node, the indices of its parent, left child, and
right child can be computed simply:
• A[1] is the root of the tree
Basic Operations - Continued
• the LEFT procedure can compute 2i in one instruction by simply
shifting the binary representation of i left by one bit position
• The RIGHT procedure can quickly compute 2i + 1 by shifting the
binary representation of i left by one bit position and then adding in a
1 as the low-order bit.
• The PARENT procedure can compute Floor(i/2) by shifting i right one
bit position.
Heap Computation
Heap property
• Heap property
• The property that the values in the node must satisfy
• Max-heap property, for every node i other than the root
• A[PARENT(i)] ≥ A[i]
• The value of a node is at most the value of its parent
• The largest element in a max-heap is stored at the root
• The sub-tree rooted at a node contains values no larger than that contained
at the node itself
Max and min heaps
Heap Height
• The height of a node in a heap
• The number of edges on the longest simple downward path from the node to
a leaf
• The height of a heap is the height of its root
• The height of a heap of n elements is Θ (lg n)
• For example
• Height of node 2 is 2
• Height of the heap is 3
Heap Procedures
• MAX-HEAPIFY
• Maintains the max-heap property
• O(lg n)
• BUILD-MAX-HEAP
• Produces a max-heap from an unordered input array
• O(n)
• HEAPSORT
• Sorts an array in place
• O(n lg n)
Maintaining the Heap Property
• MAX-HEAPIFY is an important subroutine to maintain the max-heap
property
• Inputs: an array A and an index i into the array
• Outputs: the sub-tree rooted at index I becomes a max-heap
• Assume: the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but
A[i] may be smaller than its children
• violate the max-heap property
• Method: MAX-HEAPIFY let the value at A[i] floats down in the max-heap
Example of MAX-HEAPIFY
Example of MAX-HEAPIFY
Example of MAX-HEAPIFY
MAX-HEAPIFY
Extract the indices of LEFT and RIGHT
children of i
Choose the largest of A[i], A[l], A[r]
Float down A[i] recursively
Running Time of MAX-HEAPIFY
• Θ(1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)]
• Plus the time to run MAX-HEAPIFY on a sub-tree rooted at one of the
children of node I
• The children’s sub-trees each have size at most 2n/3 (why?)
• the worst case occurs when the last row of the tree is exactly half full
• T(n) ≤ T(2n/3) + Θ(1)
• By case 2 of the master theorem
• T(n) = O(lg n)
Heapify Example
Building a Max-Heap
• Observation: A[(Floor(n/2)+1)..n] are all leaves of the tree
• Each is a 1-element heap to begin with
• Upper bound on the running time
• O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n)
• Not tight
Example
Example
Example
Correctness
• To show that Build-Heap works, we need to prove its correctness
• Invariant
• At the start of each iteration of the for loop of lines 2–3, each node i + 1, i + 2,
… , n is the root of a max-heap.
• Initialization: Prior to the first iteration of the loop, i = Floor (n/2). Each node Floor(n/2)+1,
Floor(n/2)+2,…,n is a leaf and is thus the root of a trivial max-heap.
• Maintenance: By the loop invariant, therefore, they are both roots of max-heaps. This is precisely
the condition required for the call MAX-HEAPIFY(A, i) to make node i a max-heap root. Moreover,
the MAX-HEAPIFY call preserves the property that nodes i+1, i+2,…,n are all roots of max-heaps.
Decrementing i in the for loop update reestablishes the loop invariant for the next iteration.
• Termination: At termination, i = 0. By the loop invariant, each node 1,2,…,n is the root of a
max-heap. In particular, node 1 is.
Heapsort
• Using BUILD-MAX-HEAP to build a max-heap on the input array
A[1..n], where n=length[A]
• Put the maximum element, A[1], to A[n]
• Then discard node n from the heap by decrementing heap-size(A)
• A[2..n-1] remain max-heaps, but A[1] may violate
• call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1]
• Repeat the above process from n down to 2
• Cost: O(n lg n)
• BUILD-MAX-HEAP: O(n)
• Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
Heapsort Algorithm
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Example: Heapsort
Priority queues
• A priority queue is a data structure for maintaining a set S of elements,
each with an associated value called a key. A max-priority queue supports
the following operations:
• INSERT(S, x): inserts the element x into the set S, which is equivalent to the
operation S = S U {x}
• MAXIMUM(S) returns the element of S with the largest key.
• EXTRACT-MAX(S) removes and returns the element of S with the largest
key.
• INCREASE-KEY(S,x,k) increases the value of element x’s key to the new
value k, which is assumed to be at least as large as x’s current key value.
Finding and Extracting
the maximum element
Θ (1)
O(lg n)
Increasing key value and Insert
O(lg n)
O(lg n)

More Related Content

Similar to Lecture 07 - HeapSort.pptx

Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortReetesh Gupta
 
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
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.pptAryanGour1
 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of HeapMeghaj Mallick
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingTraian Rebedea
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialAymericTaylor
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12sumitbardhan
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Anand Ingle
 

Similar to Lecture 07 - HeapSort.pptx (20)

Analysis of Algorithms-Heapsort
Analysis of Algorithms-HeapsortAnalysis of Algorithms-Heapsort
Analysis of Algorithms-Heapsort
 
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
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Sorting
SortingSorting
Sorting
 
Heaps
HeapsHeaps
Heaps
 
Heap Tree.pdf
Heap Tree.pdfHeap Tree.pdf
Heap Tree.pdf
 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.ppt
 
lecture 5
lecture 5lecture 5
lecture 5
 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptx
 
Array implementation & Construction of Heap
Array implementation & Construction of HeapArray implementation & Construction of Heap
Array implementation & Construction of Heap
 
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic ProgammingAlgorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
 
Heap sort
Heap sortHeap sort
Heap sort
 
Heaps and tries power point this is an educational material
Heaps and tries power point this is an educational materialHeaps and tries power point this is an educational material
Heaps and tries power point this is an educational material
 
Heapify
HeapifyHeapify
Heapify
 
Heap
HeapHeap
Heap
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12358 33 powerpoint-slides_12-heaps_chapter-12
358 33 powerpoint-slides_12-heaps_chapter-12
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 

Lecture 07 - HeapSort.pptx

  • 2.
  • 3. Introduction to Heapsort • Running time: O(n lg n) Like merge sort • Sorts in place: only a constant number of array elements are stored outside the input array at any time Like insertion sort • Heap A data structure used by Heapsort to manage information during the execution of the algorithm • Can be used as an efficient priority queue
  • 4. Perfect Binary Tree • For binary tree with height h All nodes at levels h–1 or less have 2 children (full)
  • 5. Complete Binary Trees • For binary tree with height h All nodes at levels h–2 or less have 2 children (full) All leaves on level h are as far left as possible
  • 7. Heaps • Two key properties • Complete binary tree • Value at node • Smaller than or equal to values in sub-trees (min-heap) • Greater than or equal to values in sub-trees (max-heap) • Example max-heap Y ≤ X Z ≤ X
  • 8. Heap and Non-heap Examples
  • 9. Binary Heap • An array object that can be viewed as a nearly complete binary tree • Each tree node corresponds to an array element that stores the value in the tree node • The tree is completely filled on all levels except possibly the lowest, which is filled from the left up to a point • A has two attributes • length[A]: number of elements in the array • heap-size[A]: number of elements in the heap stored within A heap-size[A] ≤ length[A] • max-heap and min-heap
  • 12. Basic procedures • Given the index i of a node, the indices of its parent, left child, and right child can be computed simply: • A[1] is the root of the tree
  • 13. Basic Operations - Continued • the LEFT procedure can compute 2i in one instruction by simply shifting the binary representation of i left by one bit position • The RIGHT procedure can quickly compute 2i + 1 by shifting the binary representation of i left by one bit position and then adding in a 1 as the low-order bit. • The PARENT procedure can compute Floor(i/2) by shifting i right one bit position.
  • 15. Heap property • Heap property • The property that the values in the node must satisfy • Max-heap property, for every node i other than the root • A[PARENT(i)] ≥ A[i] • The value of a node is at most the value of its parent • The largest element in a max-heap is stored at the root • The sub-tree rooted at a node contains values no larger than that contained at the node itself
  • 16. Max and min heaps
  • 17. Heap Height • The height of a node in a heap • The number of edges on the longest simple downward path from the node to a leaf • The height of a heap is the height of its root • The height of a heap of n elements is Θ (lg n) • For example • Height of node 2 is 2 • Height of the heap is 3
  • 18. Heap Procedures • MAX-HEAPIFY • Maintains the max-heap property • O(lg n) • BUILD-MAX-HEAP • Produces a max-heap from an unordered input array • O(n) • HEAPSORT • Sorts an array in place • O(n lg n)
  • 19. Maintaining the Heap Property • MAX-HEAPIFY is an important subroutine to maintain the max-heap property • Inputs: an array A and an index i into the array • Outputs: the sub-tree rooted at index I becomes a max-heap • Assume: the binary tree rooted at LEFT(i) and RIGHT(i) are max-heaps, but A[i] may be smaller than its children • violate the max-heap property • Method: MAX-HEAPIFY let the value at A[i] floats down in the max-heap
  • 23. MAX-HEAPIFY Extract the indices of LEFT and RIGHT children of i Choose the largest of A[i], A[l], A[r] Float down A[i] recursively
  • 24. Running Time of MAX-HEAPIFY • Θ(1) to find out the largest among A[i], A[LEFT(i)], and A[RIGHT(i)] • Plus the time to run MAX-HEAPIFY on a sub-tree rooted at one of the children of node I • The children’s sub-trees each have size at most 2n/3 (why?) • the worst case occurs when the last row of the tree is exactly half full • T(n) ≤ T(2n/3) + Θ(1) • By case 2 of the master theorem • T(n) = O(lg n)
  • 26. Building a Max-Heap • Observation: A[(Floor(n/2)+1)..n] are all leaves of the tree • Each is a 1-element heap to begin with • Upper bound on the running time • O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n) • Not tight
  • 30. Correctness • To show that Build-Heap works, we need to prove its correctness • Invariant • At the start of each iteration of the for loop of lines 2–3, each node i + 1, i + 2, … , n is the root of a max-heap. • Initialization: Prior to the first iteration of the loop, i = Floor (n/2). Each node Floor(n/2)+1, Floor(n/2)+2,…,n is a leaf and is thus the root of a trivial max-heap. • Maintenance: By the loop invariant, therefore, they are both roots of max-heaps. This is precisely the condition required for the call MAX-HEAPIFY(A, i) to make node i a max-heap root. Moreover, the MAX-HEAPIFY call preserves the property that nodes i+1, i+2,…,n are all roots of max-heaps. Decrementing i in the for loop update reestablishes the loop invariant for the next iteration. • Termination: At termination, i = 0. By the loop invariant, each node 1,2,…,n is the root of a max-heap. In particular, node 1 is.
  • 31. Heapsort • Using BUILD-MAX-HEAP to build a max-heap on the input array A[1..n], where n=length[A] • Put the maximum element, A[1], to A[n] • Then discard node n from the heap by decrementing heap-size(A) • A[2..n-1] remain max-heaps, but A[1] may violate • call MAX-HEAPIFY(A, 1) to restore the max-heap property for A[1..n-1] • Repeat the above process from n down to 2 • Cost: O(n lg n) • BUILD-MAX-HEAP: O(n) • Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
  • 41. Priority queues • A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key. A max-priority queue supports the following operations: • INSERT(S, x): inserts the element x into the set S, which is equivalent to the operation S = S U {x} • MAXIMUM(S) returns the element of S with the largest key. • EXTRACT-MAX(S) removes and returns the element of S with the largest key. • INCREASE-KEY(S,x,k) increases the value of element x’s key to the new value k, which is assumed to be at least as large as x’s current key value.
  • 42. Finding and Extracting the maximum element Θ (1) O(lg n)
  • 43. Increasing key value and Insert O(lg n) O(lg n)