SlideShare a Scribd company logo
TOPIC: HEAP SORT
Group: 12
Date: 13/09/2022
COURSE : CSE 2218/CSI 228 (C)
Faculty:
Sufi Aurangzeb Hossain
LECTURER (ADJUNCT)
DEPT. OF COMPUTER SCIENCE & ENGINEERING
011201230 Santi Brata Nath Joy
011201295 Md. Tanzid Ahsan
MEMBERS
011201214 Saikat Hossain
HISTORY
Sir John William Joseph Williams
1964 Instant Access
DEFINATION
Sorting Algorithm
Binary Tree Heap
O (N log N)
HEAP BINARY TREE
1
3
2
4
6
5
7
8
1
2 3
4 5
9
6 7
8 9
5
Index
Data 6 7 8 9
5 6 7 8
3
2 4
1
0 1 2 3 4
HEAP PROPERTY
Max Heap Min Heap
10
5 3
4 1
1
3 5
4 7
A[Parent(i)] ≥ A[i] A[Parent(i)] ≤ A[i]
3
5 4 1
10
0 1 2 3 4
Index
Data 5
3 4 7
1
0 1 2 3 4
PROCEDURES OF HEAP SORT
Heapify Heap Sort
Build Heap
Construct Binary Tree
HEAPIFY
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
4
10 3
10
4 3
Swap
BUILD HEAP
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
5 1
Internal Nodes
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2 //down to 0
do Heapify(A, i)
}
BUILD HEAP
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2 //down to 0
do Heapify(A, i)
}
3
5 4 1
10
0 1 2 3 4
Index
Data
10
5 3
4 1
HEAP SORT
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
0 1 2 3 4
4
3 5 10
1
Index
Data
3
5 4 1
10
0 1 2 3 4
Index
Data
0 1 2 3 4
3
5 4 1
10
Index
Data
Swap
0 1 2 3 4
3
5 4 10
1
Index
Data
METHOD
Heap Sort
Construct
Binary Tree
Build Heap
SIMULATION
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
3
10 5 1
4
0 1 2 3 4
Index
Data
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
5
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
5 1
Done
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
5 1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
3
10 5 1
4
0 1 2 3 4
Index
Data
4
10 3
5 1
10 is greater than 4
then swap A[i] <-A[largest]
Swap 4 and 10
Swap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
3
4 5 1
10
0 1 2 3 4
Index
Data
10
4 3
5 1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
3
4 5 1
10
0 1 2 3 4
Index
Data
5 is greater than 4
then swap A[i] <-A[largest]
Swap 5 and 4
10
4 3
5 1
Swap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
3
5 4 1
10
0 1 2 3 4
Index
Data
10
5 3
4 1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
3
5 4 1
10
0 1 2 3 4
Index
Data
10
5 3
4 1
After Build Heap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 1
10
Index
Data
10
5 3
4 1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A] //down to 2
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 1
10
Index
Data
10
5 3
4 1
Swap
Swapping the first
element with the last
element of unsorted
array
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 10
1
Index
Data
1
5 3
4 10 Deleting the last element as sorted
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 10
1
Index
Data
1
5 3
4
Sorted Array
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 10
1
Index
Data
1
5 3
4
Calling Heapify
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
5 4 10
1
Index
Data
1
5 3
4
5 is greater than 1
then swap A[i] <-A[largest]
Swap 5 and 1
Swap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
1 4 10
5
Index
Data
5
1 3
4
5 is greater than 1
then swap A[i] <-A[largest]
Swap 5 and 1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
1 4 10
5
Index
Data
5
1 3
4
4 is greater than 1
then swap A[i] <-A[largest]
Swap 4 and 1
Swap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
4 1 10
5
Index
Data
5
4 3
1
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
Swap
SIMULATION
0 1 2 3 4
3
4 1 10
5
Index
Data
5
4 3
1
Swapping the first
element with the last
element of unsorted
array and delete the
last element as
sorted
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A] //down to 2
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
4 5 10
1
Index
Data
1
4 3
Sorted Array
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
4 5 10
1
Index
Data
1
4 3
4 is greater than 1
then swap A[i] <-A[largest]
Swap 4 and 1
Swap
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
1 5 10
4
Index
Data
4
1 3
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
3
1 5 10
4
Index
Data
4
1 3
Swap
Swapping the first
element with the last
element of unsorted
array and delete the
last element as
sorted
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
SIMULATION
0 1 2 3 4
4
1 5 10
3
Index
Data
3
1
Build
Heap
Max
Heap
Sort
Continue…
SIMULATION
0 1 2 3 4
4
3 5 10
1
Index
Data
The array is sorted
Heapify(A, i)
{
l <- left(i)
r <- right(i)
if l <= heapsize[A] and A[l] > A[i]
then largest <- l
else largest <- i
if r <= heapsize[A] and A[r] > A[largest]
then largest <- r
if largest != i
then swap A[i] <-A[largest]
Heapify(A, largest)
}
Buildheap(A)
{
heapsize[A] length[A]
for i |length[A]/2
do Heapify(A, i)
}
Heapsort(A)
{
Buildheap(A)
for i  length[A]
do swap A[1]  A[i]
heapsize[A]  heapsize[A] - 1
Heapify(A, 1)
}
TIME COMPLEXITY ANALYSIS
O (N)
O(log N)
O (N log N)
(n – 1) O(N log N)
SPACE COMPLEXITY ANALYSIS
O (1)
APPLICATION
ADVANTAGES
• No quadratic worst-case run time.
• In-place sorting algorithm, performs sorting in O(1)
space complexity.
• Compared to quicksort, it has a better worst-case time
complexity – O(NlogN).
• The best-case complexity is the same for both quick
and heap sort – O(NlogN).
• Unlike merge sort, it doesn’t require extra space.
• The input data being completely or almost sorted
doesn’t make the complexities suffer.
• The average-case time complexity is the same as that
of merge quick and quick sort
DISADVANTAGES
• Heap sort is typically not stable since the operations
on the top can change the relative order of equal key
items. It’s typically an unstable algorithm.
• If the input array is very large and doesn’t fit into the
memory and partitioning the array is faster than
maintaining the heap, heap sort isn’t an option. In
such cases, something like merge sort or bucket sort
where parts of the array can be processed separately
and parallelly, works best.
Thank you
Doubts?

More Related Content

More from Santi Brata Nath Joy

Defence Presentation United International University.pptx
Defence Presentation United International University.pptxDefence Presentation United International University.pptx
Defence Presentation United International University.pptx
Santi Brata Nath Joy
 
Camera Ready Submission Oral Presentation.pptx
Camera Ready Submission Oral Presentation.pptxCamera Ready Submission Oral Presentation.pptx
Camera Ready Submission Oral Presentation.pptx
Santi Brata Nath Joy
 
Pre-Defence Presentation.pptx
Pre-Defence Presentation.pptxPre-Defence Presentation.pptx
Pre-Defence Presentation.pptx
Santi Brata Nath Joy
 
UIU Recruiter Project Proposal.pptx
UIU Recruiter Project Proposal.pptxUIU Recruiter Project Proposal.pptx
UIU Recruiter Project Proposal.pptx
Santi Brata Nath Joy
 
Entrepreneurship
Entrepreneurship Entrepreneurship
Entrepreneurship
Santi Brata Nath Joy
 
Android architecture
Android architectureAndroid architecture
Android architecture
Santi Brata Nath Joy
 
Android
Android Android

More from Santi Brata Nath Joy (7)

Defence Presentation United International University.pptx
Defence Presentation United International University.pptxDefence Presentation United International University.pptx
Defence Presentation United International University.pptx
 
Camera Ready Submission Oral Presentation.pptx
Camera Ready Submission Oral Presentation.pptxCamera Ready Submission Oral Presentation.pptx
Camera Ready Submission Oral Presentation.pptx
 
Pre-Defence Presentation.pptx
Pre-Defence Presentation.pptxPre-Defence Presentation.pptx
Pre-Defence Presentation.pptx
 
UIU Recruiter Project Proposal.pptx
UIU Recruiter Project Proposal.pptxUIU Recruiter Project Proposal.pptx
UIU Recruiter Project Proposal.pptx
 
Entrepreneurship
Entrepreneurship Entrepreneurship
Entrepreneurship
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Android
Android Android
Android
 

Recently uploaded

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
architagupta876
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 

Recently uploaded (20)

ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
AI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptxAI assisted telemedicine KIOSK for Rural India.pptx
AI assisted telemedicine KIOSK for Rural India.pptx
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 

Heap Sort.ppx

  • 1. TOPIC: HEAP SORT Group: 12 Date: 13/09/2022 COURSE : CSE 2218/CSI 228 (C) Faculty: Sufi Aurangzeb Hossain LECTURER (ADJUNCT) DEPT. OF COMPUTER SCIENCE & ENGINEERING 011201230 Santi Brata Nath Joy 011201295 Md. Tanzid Ahsan MEMBERS 011201214 Saikat Hossain
  • 2. HISTORY Sir John William Joseph Williams 1964 Instant Access
  • 4. HEAP BINARY TREE 1 3 2 4 6 5 7 8 1 2 3 4 5 9 6 7 8 9 5 Index Data 6 7 8 9 5 6 7 8 3 2 4 1 0 1 2 3 4
  • 5. HEAP PROPERTY Max Heap Min Heap 10 5 3 4 1 1 3 5 4 7 A[Parent(i)] ≥ A[i] A[Parent(i)] ≤ A[i] 3 5 4 1 10 0 1 2 3 4 Index Data 5 3 4 7 1 0 1 2 3 4
  • 6. PROCEDURES OF HEAP SORT Heapify Heap Sort Build Heap Construct Binary Tree
  • 7. HEAPIFY Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } 4 10 3 10 4 3 Swap
  • 8. BUILD HEAP 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 5 1 Internal Nodes Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 //down to 0 do Heapify(A, i) }
  • 9. BUILD HEAP Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 //down to 0 do Heapify(A, i) } 3 5 4 1 10 0 1 2 3 4 Index Data 10 5 3 4 1
  • 10. HEAP SORT Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) } 0 1 2 3 4 4 3 5 10 1 Index Data 3 5 4 1 10 0 1 2 3 4 Index Data 0 1 2 3 4 3 5 4 1 10 Index Data Swap 0 1 2 3 4 3 5 4 10 1 Index Data
  • 12. SIMULATION PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1 3 10 5 1 4 0 1 2 3 4 Index Data
  • 13. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1
  • 14. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1
  • 15. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1
  • 16. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 5 PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1
  • 17. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 5 1 Done PARENT (i) return floor(i/2) LEFT (i) return 2i RIGHT (i) return 2i + 1
  • 18. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 5 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 19. SIMULATION 3 10 5 1 4 0 1 2 3 4 Index Data 4 10 3 5 1 10 is greater than 4 then swap A[i] <-A[largest] Swap 4 and 10 Swap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 20. SIMULATION 3 4 5 1 10 0 1 2 3 4 Index Data 10 4 3 5 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 21. SIMULATION 3 4 5 1 10 0 1 2 3 4 Index Data 5 is greater than 4 then swap A[i] <-A[largest] Swap 5 and 4 10 4 3 5 1 Swap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 22. SIMULATION 3 5 4 1 10 0 1 2 3 4 Index Data 10 5 3 4 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 23. SIMULATION 3 5 4 1 10 0 1 2 3 4 Index Data 10 5 3 4 1 After Build Heap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 24. SIMULATION 0 1 2 3 4 3 5 4 1 10 Index Data 10 5 3 4 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] //down to 2 do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 25. SIMULATION 0 1 2 3 4 3 5 4 1 10 Index Data 10 5 3 4 1 Swap Swapping the first element with the last element of unsorted array Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 26. SIMULATION 0 1 2 3 4 3 5 4 10 1 Index Data 1 5 3 4 10 Deleting the last element as sorted Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 27. SIMULATION 0 1 2 3 4 3 5 4 10 1 Index Data 1 5 3 4 Sorted Array Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 28. SIMULATION 0 1 2 3 4 3 5 4 10 1 Index Data 1 5 3 4 Calling Heapify Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 29. SIMULATION 0 1 2 3 4 3 5 4 10 1 Index Data 1 5 3 4 5 is greater than 1 then swap A[i] <-A[largest] Swap 5 and 1 Swap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 30. SIMULATION 0 1 2 3 4 3 1 4 10 5 Index Data 5 1 3 4 5 is greater than 1 then swap A[i] <-A[largest] Swap 5 and 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 31. SIMULATION 0 1 2 3 4 3 1 4 10 5 Index Data 5 1 3 4 4 is greater than 1 then swap A[i] <-A[largest] Swap 4 and 1 Swap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 32. SIMULATION 0 1 2 3 4 3 4 1 10 5 Index Data 5 4 3 1 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 33. Swap SIMULATION 0 1 2 3 4 3 4 1 10 5 Index Data 5 4 3 1 Swapping the first element with the last element of unsorted array and delete the last element as sorted Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] //down to 2 do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 34. SIMULATION 0 1 2 3 4 3 4 5 10 1 Index Data 1 4 3 Sorted Array Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 35. SIMULATION 0 1 2 3 4 3 4 5 10 1 Index Data 1 4 3 4 is greater than 1 then swap A[i] <-A[largest] Swap 4 and 1 Swap Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 36. SIMULATION 0 1 2 3 4 3 1 5 10 4 Index Data 4 1 3 Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 37. SIMULATION 0 1 2 3 4 3 1 5 10 4 Index Data 4 1 3 Swap Swapping the first element with the last element of unsorted array and delete the last element as sorted Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) }
  • 38. SIMULATION 0 1 2 3 4 4 1 5 10 3 Index Data 3 1 Build Heap Max Heap Sort Continue…
  • 39. SIMULATION 0 1 2 3 4 4 3 5 10 1 Index Data The array is sorted
  • 40. Heapify(A, i) { l <- left(i) r <- right(i) if l <= heapsize[A] and A[l] > A[i] then largest <- l else largest <- i if r <= heapsize[A] and A[r] > A[largest] then largest <- r if largest != i then swap A[i] <-A[largest] Heapify(A, largest) } Buildheap(A) { heapsize[A] length[A] for i |length[A]/2 do Heapify(A, i) } Heapsort(A) { Buildheap(A) for i  length[A] do swap A[1]  A[i] heapsize[A]  heapsize[A] - 1 Heapify(A, 1) } TIME COMPLEXITY ANALYSIS O (N) O(log N) O (N log N) (n – 1) O(N log N)
  • 43. ADVANTAGES • No quadratic worst-case run time. • In-place sorting algorithm, performs sorting in O(1) space complexity. • Compared to quicksort, it has a better worst-case time complexity – O(NlogN). • The best-case complexity is the same for both quick and heap sort – O(NlogN). • Unlike merge sort, it doesn’t require extra space. • The input data being completely or almost sorted doesn’t make the complexities suffer. • The average-case time complexity is the same as that of merge quick and quick sort
  • 44. DISADVANTAGES • Heap sort is typically not stable since the operations on the top can change the relative order of equal key items. It’s typically an unstable algorithm. • If the input array is very large and doesn’t fit into the memory and partitioning the array is faster than maintaining the heap, heap sort isn’t an option. In such cases, something like merge sort or bucket sort where parts of the array can be processed separately and parallelly, works best.