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
ย 
Sorting
SortingSorting
Sortingvatsaanadi
ย 
21. Heap_new.ppt
21. Heap_new.ppt21. Heap_new.ppt
21. Heap_new.pptAryanGour1
ย 
lecture 5
lecture 5lecture 5
lecture 5sajinsc
ย 
HEAP SORT .pptx
HEAP SORT .pptxHEAP SORT .pptx
HEAP SORT .pptxFazlullah28
ย 
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
ย 
Heap sort
Heap sortHeap sort
Heap sortAyesha Tahir
ย 
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
ย 
Heapify
HeapifyHeapify
Heapifyhimank31
ย 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
ย 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05ashish bansal
ย 
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 Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
ย 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
ย 
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
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธDelhi Call girls
ย 
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
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...OnePlan Solutions
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
ย 
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
ย 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
ย 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
ย 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.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
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
ย 
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
ย 
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
ย 
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Steffen Staab
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfWilly Marroquin (WillyDevNET)
ย 

Recently uploaded (20)

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female serviceCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Badshah Nagar Lucknow best Female service
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
ย 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
ย 
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
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
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 โ˜‚๏ธ
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
ย 
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
ย 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
ย 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
ย 
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
ย 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
ย 
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
ย 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
ย 
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
ย 
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...
ย 
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spacesย - and Epistemic Querying of RDF-...
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.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)