SlideShare a Scribd company logo
Menu
• Priority Queues
• Heaps
• Heapsort
Priority Queue
A data structure implementing a set S of elements, each associated with a
key, supporting the following operations:
increase_key(S, x, k) :
insert element x into set S
return element of S with largest key return
element of S with largest key and remove it
from S
increase the value of element x’ s key to new
value k
(assumed to be as large as current value)
insert(S, x) :
max(S) :
extract_max(S) :
Heap
• Implementation of a priority queue
• An array, visualized as a nearly complete binary tree
•Max Heap Property: The key of a node is≥ than the keys of its children
(Min Heap defined analogously)
161410 8 7 9 3 2 4 1
1 2 3 4 5 6 7 8 9 10
10
8 9
2 4
1
16
1
2
14
3
4
5
7
6 7
3
8 9 10
Heap as a Tree
root of tree. :first element in the array, corresponding to i = 1
parent(i) =i/2: returns index of node's parent
left(i)=2i. : returns index of node's left child
right(i)=2i+1: returns index of node's right child
10
8 9
16
1
2
14
161410 8 7 9 3 2 4 1
1 2 3 4 5 6 7 8 9 10
3
4
5
7
6 7
3
8 9 10
2 4 1
Heap Operations
insert, extract_max, heapsort
produce a max-heap from an unordered
array
correct a single violation of the
heap property in a subtree at its root
build_max_heap :
max_heapify :
Max_heapify (Example)
Max_heapify (Example)
Max_heapify (Example)
Time=? O(log n)
Max_Heapify Pseudocode
Max_Heapify(A,i):
l = left(i)
r = right(i)
if (l <= heap-size(A) and A[l] > A[i])
then largest = l
else largest = i
if (r <= heap-size(A) and A[r] > A[largest])
then largest = r
if largest != i
then exchange A[i] and A[largest]
Max_Heapify(A, largest)
Build_Max_Heap(A)
• Converts A[1…n] to a max heap
• Build_Max_Heap(A):
• heap-size(A) = length(A)
for i=n/2 downto 1
do Max_Heapify(A, i)
• Why start at n/2?
• Because elements A[n/2 + 1 … n] are all leaves of the tree 2i > n, for i > n/2 +
1
Time= O(n log n) via simple analysis
Build-Max-Heap Demo
Build-Max-Heap Demo
Build-Max-Heap
Heap-Sort
Sorting Strategy:
1. Build Max Heap from unordered array;
2. Find maximum element A[1];
3. Swap elements A[n] and A[1]:
now max element is at the end of the array!
4. Discard node n from heap
(by decrementing heap-size variable)
5.New root may violate max heap property, but its children are max
heaps. Run max_heapify to fix this.
6. Go to Step 2 unless heap is empty.
Heap-Sort Demo
Swap A[10] and
A[1]
Max_heapify(A,1)
Heap-Sort Demo
Swap A[9] and
A[1]
Heap-Sort Demo
Heap-Sort Demo
Swap A[8] and
A[1]
Heap-Sort
• Running time:
• after n iterations the Heap is empty
• every iteration involves a swap and a max_heapify operation; hence it
takes O(log n) time
Overall O(n log n)

More Related Content

PPTX
Algorithms - "heap sort"
PPT
Cis435 week05
PDF
Heapsort quick sort
PDF
Heap and heapsort
PPT
Analysis of Algorithms-Heapsort
PPT
Unit III Heaps.ppt
PPT
PPTX
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming
Algorithms - "heap sort"
Cis435 week05
Heapsort quick sort
Heap and heapsort
Analysis of Algorithms-Heapsort
Unit III Heaps.ppt
Algorithm Design and Complexity - Course 4 - Heaps and Dynamic Progamming

Similar to Heap Sort sorting algorithm analysis of algorithm.pptx (20)

PPT
lecture 5
PPTX
Binary Heap Tree, Data Structure
PDF
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
PDF
Heap Tree.pdf
PPT
21. Heap_new.ppt
PPT
Applicationof datastructures
PPT
Applicationof datastructures
PPT
lecture 6
PPTX
Lecture 07 - HeapSort.pptx
PPTX
DA_02_algorithms.pptx
PPT
Heapsort ppt
PDF
USING JAVAThere are at least two types of nearly sorted array.pdf
PPT
thisisheapsortpptfilewhichyoucanuseanywhereanytim
PPTX
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
PDF
03-data-structures.pdf
PPTX
Lecture 5_ Sorting and order statistics.pptx
PPTX
Sorting
PPTX
heapsort_bydinesh
PDF
4 heapsort pq
lecture 5
Binary Heap Tree, Data Structure
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
Heap Tree.pdf
21. Heap_new.ppt
Applicationof datastructures
Applicationof datastructures
lecture 6
Lecture 07 - HeapSort.pptx
DA_02_algorithms.pptx
Heapsort ppt
USING JAVAThere are at least two types of nearly sorted array.pdf
thisisheapsortpptfilewhichyoucanuseanywhereanytim
week2.v2 dsfjue0owirewoifudsoufsoiuewrew.pptx
03-data-structures.pdf
Lecture 5_ Sorting and order statistics.pptx
Sorting
heapsort_bydinesh
4 heapsort pq
Ad

Recently uploaded (20)

PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
web development for engineering and engineering
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
additive manufacturing of ss316l using mig welding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Safety Seminar civil to be ensured for safe working.
PPT
Project quality management in manufacturing
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Artificial Intelligence
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
web development for engineering and engineering
R24 SURVEYING LAB MANUAL for civil enggi
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original
additive manufacturing of ss316l using mig welding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Safety Seminar civil to be ensured for safe working.
Project quality management in manufacturing
bas. eng. economics group 4 presentation 1.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Artificial Intelligence
Internet of Things (IOT) - A guide to understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
Ad

Heap Sort sorting algorithm analysis of algorithm.pptx

  • 1. Menu • Priority Queues • Heaps • Heapsort
  • 2. Priority Queue A data structure implementing a set S of elements, each associated with a key, supporting the following operations: increase_key(S, x, k) : insert element x into set S return element of S with largest key return element of S with largest key and remove it from S increase the value of element x’ s key to new value k (assumed to be as large as current value) insert(S, x) : max(S) : extract_max(S) :
  • 3. Heap • Implementation of a priority queue • An array, visualized as a nearly complete binary tree •Max Heap Property: The key of a node is≥ than the keys of its children (Min Heap defined analogously) 161410 8 7 9 3 2 4 1 1 2 3 4 5 6 7 8 9 10 10 8 9 2 4 1 16 1 2 14 3 4 5 7 6 7 3 8 9 10
  • 4. Heap as a Tree root of tree. :first element in the array, corresponding to i = 1 parent(i) =i/2: returns index of node's parent left(i)=2i. : returns index of node's left child right(i)=2i+1: returns index of node's right child 10 8 9 16 1 2 14 161410 8 7 9 3 2 4 1 1 2 3 4 5 6 7 8 9 10 3 4 5 7 6 7 3 8 9 10 2 4 1
  • 5. Heap Operations insert, extract_max, heapsort produce a max-heap from an unordered array correct a single violation of the heap property in a subtree at its root build_max_heap : max_heapify :
  • 9. Max_Heapify Pseudocode Max_Heapify(A,i): l = left(i) r = right(i) if (l <= heap-size(A) and A[l] > A[i]) then largest = l else largest = i if (r <= heap-size(A) and A[r] > A[largest]) then largest = r if largest != i then exchange A[i] and A[largest] Max_Heapify(A, largest)
  • 10. Build_Max_Heap(A) • Converts A[1…n] to a max heap • Build_Max_Heap(A): • heap-size(A) = length(A) for i=n/2 downto 1 do Max_Heapify(A, i) • Why start at n/2? • Because elements A[n/2 + 1 … n] are all leaves of the tree 2i > n, for i > n/2 + 1 Time= O(n log n) via simple analysis
  • 14. Heap-Sort Sorting Strategy: 1. Build Max Heap from unordered array; 2. Find maximum element A[1]; 3. Swap elements A[n] and A[1]: now max element is at the end of the array! 4. Discard node n from heap (by decrementing heap-size variable) 5.New root may violate max heap property, but its children are max heaps. Run max_heapify to fix this. 6. Go to Step 2 unless heap is empty.
  • 15. Heap-Sort Demo Swap A[10] and A[1] Max_heapify(A,1)
  • 19. Heap-Sort • Running time: • after n iterations the Heap is empty • every iteration involves a swap and a max_heapify operation; hence it takes O(log n) time Overall O(n log n)