SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
12/08/2006 CMSC 131 Fall 2006
Rance Cleaveland
©2006 Univeristy of Maryland
Lecture 41:
Heapsort
Last time:
1. Analyzing selection sort
2. Trees
Today:
1. Project #8 assigned!
2. Heaps
3. Heapsort
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
1
Recall: Binary Trees
Data structures consisting of
Nodes (where data it stored)
Edges
Every node has one parent except
root, which has none
Every node has ≤ two children
Nodes with no children = leaves
Nodes with children = internal
Nodes can be divided into levels
based on distance from root
The height of a tree is the longest
path from root
A binary tree is complete if:
Every level except the last is full
In the last level, every leaf is as far to
the left as possible
A binary tree is perfect if every level
(including the last) is full
‘a’
‘y’ ‘J’
‘3’‘c’
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
2
The Tree Quiz
1. Let T be a perfect tree of height n
a. How many leaves does T have?
2n
b. How many internal nodes does T have?
2n - 1
c. How many total nodes does T have?
2n+1 - 1
2. Let T be a complete binary tree of
height n
a. How many leaves does T have (give
range)?
2n-1 to 2n, inclusive
b. How many total nodes may T have (give
range)?
2n to 2n+1 - 1, inclusive
3. Let T be a complete binary tree with n
nodes. What is its height?
Floor (rounding down) of log2n
‘a’
‘y’ ‘J’
‘3’‘c’
‘z’ ‘:’
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
3
Heaps
Assume data stored in
tree nodes is ordered
by ≤
A heap is:
A complete binary tree
such that:
Every parent’s data is ≥
its child(ren)’s data
Fact: greatest value is
stored at root!
17
14 11
1015
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
4
Heapsort
Idea:
Keep unsorted elements in a heap
Keep sorted elements in array, filling in
from right (largest) to left (smallest)
Maintain following property:
Every unsorted element ≤ every sorted
element (cf. selection sort)
To increase sorted part:
Remove root (largest element) from heap
and put into correct position in array
Restore remaining heap elements into a
heap
Questions
How do you “restore” a heap?
How do you create a heap in the first
place?
17
14 11
1015
Unsorted elements
242219
Sorted elements
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
5
Restoring a Heap
After removing root of a
heap …
… how do we
rearrange the
remainder into a heap?
One approach:
Move bottom-most, right-
most leaf to root
Repeatedly swap node
with greater of children
until heap property
restored
17
14 11
1015
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
6
Example
11
14
1015
14 11
1015
15
14
1011
15
11
1014
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
7
What Is Complexity of
Restoring a Heap?
Each swap operation takes O(1)
Swap always results in level of “out-of-place”
node increasing
How many times can level increase, in terms
of n = # of nodes in tree?
log2n!
So complexity of restoration operation is
O(log2n)
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
8
Creating a Heap
To create a heap, we repeatedly insert
elements from original array into heap
How to insert a new node into a heap?
Put new node into heap as bottom-most, right-
most leaf
Swap node with parent until heap property
restored
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
9
Example: Inserting 13
17
14 11
1015
13
17
11
1315
1014
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
10
Complexity of Insertion
Each swap is O(1)
Swap decreases level of new node by one
So complexity of insertion in terms of n = # of
nodes in heap is:
O(log2n)
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
11
Complexity of Heap Creation
Recall heap-creation procedure (a is array to
be sorted)
start with empty heap h
for i = 0, …, a.length
insert a[i] into h
Complexity analysis
Insertion called for each element in a (so, n times)
Each insertion costs O(log2n)
So complexity of heap creation is O(n log2n)
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
12
Complexity of Heapsort
Here is heapsort pseudo-code for sorting array a
Create heap h from a
for j = a.length-1, …, 0 do
remove root from h and assign to a[j]
restore heap
Complexity analysis
Heap creation: O(n log2n)
Each loop iteration: O(log2n)
Number of iterations: n
Total: O(n log2n)!
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
13
0
1,000
2,000
3,000
4,000
5,000
6,000
7,000
0 8 16 24 32 40 48 56 64 72 80
Selection sort
Heapsort
Comparing Sorts
# of elements in array
runningtime
CMSC 131 Fall 2006
Rance Cleaveland
©2006 University of Maryland
14
How To Implement Heaps?
The heap can also be stored
in a, along with sorted portion
Root is stored in a[0]
Level 1 is stored in a[1], a[2]
Level 2 is stored in a[3] – a[6]
etc.
Using this scheme:
Children of node stored in a[i]
are a[2i+1], a[2i+2]
Parent of node stored in a[i] is
a[(i-1) / 2]
17
14 11
1015
242219
2422191114101517
heap sorted

Contenu connexe

Similaire à Heap

CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.pptAmitShou
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdfNash229987
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsRESHAN FARAZ
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmLearning Courses Online
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
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
 
Time complexity of union find
Time complexity of union findTime complexity of union find
Time complexity of union findWei (Terence) Li
 
Skiena algorithm 2007 lecture07 heapsort priority queues
Skiena algorithm 2007 lecture07 heapsort priority queuesSkiena algorithm 2007 lecture07 heapsort priority queues
Skiena algorithm 2007 lecture07 heapsort priority queueszukun
 

Similaire à Heap (20)

heaps2
heaps2heaps2
heaps2
 
CSE680-06HeapSort.ppt
CSE680-06HeapSort.pptCSE680-06HeapSort.ppt
CSE680-06HeapSort.ppt
 
Heapsort
HeapsortHeapsort
Heapsort
 
3.7 heap sort
3.7 heap sort3.7 heap sort
3.7 heap sort
 
03-data-structures.pdf
03-data-structures.pdf03-data-structures.pdf
03-data-structures.pdf
 
Heap tree
Heap treeHeap tree
Heap tree
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
DSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) QuestionsDSA (Data Structure and Algorithm) Questions
DSA (Data Structure and Algorithm) Questions
 
Heapsort ppt
Heapsort pptHeapsort ppt
Heapsort ppt
 
Cis435 week05
Cis435 week05Cis435 week05
Cis435 week05
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
chapt11.ppt
chapt11.pptchapt11.ppt
chapt11.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
 
Time complexity of union find
Time complexity of union findTime complexity of union find
Time complexity of union find
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Skiena algorithm 2007 lecture07 heapsort priority queues
Skiena algorithm 2007 lecture07 heapsort priority queuesSkiena algorithm 2007 lecture07 heapsort priority queues
Skiena algorithm 2007 lecture07 heapsort priority queues
 
Heaps
HeapsHeaps
Heaps
 
Q
QQ
Q
 

Dernier

Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfTechSoup
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRATanmoy Mishra
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxDr. Santhosh Kumar. N
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17Celine George
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17Celine George
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 

Dernier (20)

Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdfMaximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
Maximizing Impact_ Nonprofit Website Planning, Budgeting, and Design.pdf
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRADUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
DUST OF SNOW_BY ROBERT FROST_EDITED BY_ TANMOY MISHRA
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
M-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptxM-2- General Reactions of amino acids.pptx
M-2- General Reactions of amino acids.pptx
 
How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17How to Show Error_Warning Messages in Odoo 17
How to Show Error_Warning Messages in Odoo 17
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17How to Use api.constrains ( ) in Odoo 17
How to Use api.constrains ( ) in Odoo 17
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 

Heap

  • 1. 12/08/2006 CMSC 131 Fall 2006 Rance Cleaveland ©2006 Univeristy of Maryland Lecture 41: Heapsort Last time: 1. Analyzing selection sort 2. Trees Today: 1. Project #8 assigned! 2. Heaps 3. Heapsort
  • 2. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 1 Recall: Binary Trees Data structures consisting of Nodes (where data it stored) Edges Every node has one parent except root, which has none Every node has ≤ two children Nodes with no children = leaves Nodes with children = internal Nodes can be divided into levels based on distance from root The height of a tree is the longest path from root A binary tree is complete if: Every level except the last is full In the last level, every leaf is as far to the left as possible A binary tree is perfect if every level (including the last) is full ‘a’ ‘y’ ‘J’ ‘3’‘c’
  • 3. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 2 The Tree Quiz 1. Let T be a perfect tree of height n a. How many leaves does T have? 2n b. How many internal nodes does T have? 2n - 1 c. How many total nodes does T have? 2n+1 - 1 2. Let T be a complete binary tree of height n a. How many leaves does T have (give range)? 2n-1 to 2n, inclusive b. How many total nodes may T have (give range)? 2n to 2n+1 - 1, inclusive 3. Let T be a complete binary tree with n nodes. What is its height? Floor (rounding down) of log2n ‘a’ ‘y’ ‘J’ ‘3’‘c’ ‘z’ ‘:’
  • 4. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 3 Heaps Assume data stored in tree nodes is ordered by ≤ A heap is: A complete binary tree such that: Every parent’s data is ≥ its child(ren)’s data Fact: greatest value is stored at root! 17 14 11 1015
  • 5. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 4 Heapsort Idea: Keep unsorted elements in a heap Keep sorted elements in array, filling in from right (largest) to left (smallest) Maintain following property: Every unsorted element ≤ every sorted element (cf. selection sort) To increase sorted part: Remove root (largest element) from heap and put into correct position in array Restore remaining heap elements into a heap Questions How do you “restore” a heap? How do you create a heap in the first place? 17 14 11 1015 Unsorted elements 242219 Sorted elements
  • 6. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 5 Restoring a Heap After removing root of a heap … … how do we rearrange the remainder into a heap? One approach: Move bottom-most, right- most leaf to root Repeatedly swap node with greater of children until heap property restored 17 14 11 1015
  • 7. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 6 Example 11 14 1015 14 11 1015 15 14 1011 15 11 1014
  • 8. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 7 What Is Complexity of Restoring a Heap? Each swap operation takes O(1) Swap always results in level of “out-of-place” node increasing How many times can level increase, in terms of n = # of nodes in tree? log2n! So complexity of restoration operation is O(log2n)
  • 9. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 8 Creating a Heap To create a heap, we repeatedly insert elements from original array into heap How to insert a new node into a heap? Put new node into heap as bottom-most, right- most leaf Swap node with parent until heap property restored
  • 10. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 9 Example: Inserting 13 17 14 11 1015 13 17 11 1315 1014
  • 11. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 10 Complexity of Insertion Each swap is O(1) Swap decreases level of new node by one So complexity of insertion in terms of n = # of nodes in heap is: O(log2n)
  • 12. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 11 Complexity of Heap Creation Recall heap-creation procedure (a is array to be sorted) start with empty heap h for i = 0, …, a.length insert a[i] into h Complexity analysis Insertion called for each element in a (so, n times) Each insertion costs O(log2n) So complexity of heap creation is O(n log2n)
  • 13. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 12 Complexity of Heapsort Here is heapsort pseudo-code for sorting array a Create heap h from a for j = a.length-1, …, 0 do remove root from h and assign to a[j] restore heap Complexity analysis Heap creation: O(n log2n) Each loop iteration: O(log2n) Number of iterations: n Total: O(n log2n)!
  • 14. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 13 0 1,000 2,000 3,000 4,000 5,000 6,000 7,000 0 8 16 24 32 40 48 56 64 72 80 Selection sort Heapsort Comparing Sorts # of elements in array runningtime
  • 15. CMSC 131 Fall 2006 Rance Cleaveland ©2006 University of Maryland 14 How To Implement Heaps? The heap can also be stored in a, along with sorted portion Root is stored in a[0] Level 1 is stored in a[1], a[2] Level 2 is stored in a[3] – a[6] etc. Using this scheme: Children of node stored in a[i] are a[2i+1], a[2i+2] Parent of node stored in a[i] is a[(i-1) / 2] 17 14 11 1015 242219 2422191114101517 heap sorted