SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Ähnlich wie 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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Kürzlich hochgeladen (20)

Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

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