This document describes the heap sort algorithm. It begins by explaining heaps and their properties like the heap property. It then describes key heap operations like Max-Heapify, Build-Max-Heap, finding the maximum element, extract max, increase key, and insert. These operations allow heaps to function as priority queues. Heap sort works by building a max heap from the input array using Build-Max-Heap, then repeatedly extracting the maximum element and putting it in its sorted position. This runs in O(n log n) time, combining advantages of insertion and merge sort by sorting in-place like insertion sort but with a faster running time of merge sort.
The heap data structure is a nearly complete binary tree implemented as an array. There are two types of heaps: max-heaps and min-heaps. The MAX-HEAPIFY algorithm maintains the heap property by allowing a value to "float down" the tree. BUILD-MAX-HEAP builds a max-heap by calling MAX-HEAPIFY on each node, and HEAPSORT sorts an array using the heap structure.
The document discusses algorithms including heapsort and quicksort. It provides explanations of heap data structures and how they can be used to implement priority queues. It then describes how priority queues can be used to simulate event-driven systems like collisions in a combat billiards simulation. Quicksort is also summarized, including the key partition step that divides the array into two subarrays.
The document discusses priority queues and quicksort. It defines a priority queue as a data structure that maintains a set of elements with associated keys. Heaps can be used to implement priority queues. There are two types: max-priority queues and min-priority queues. Priority queues have applications in job scheduling and event-driven simulation. Quicksort works by partitioning an array around a pivot element and recursively sorting the sub-arrays.
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
The document discusses various sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, quicksort and heap sort. It provides pseudocode to explain the algorithms. For each algorithm, it explains the basic approach, complexity analysis and provides an example to illustrate the steps. Quicksort is explained in more detail with pseudocode and examples to demonstrate how it works by picking a pivot element, partitioning the array and recursively sorting the sub-arrays.
Array ADT(Abstract Data Type)|Data StructureAkash Gaur
The document discusses abstract data types (ADTs) and describes an array ADT. It lists common array operations like display, add, insert, delete, search, get, set, max, min, reverse, shift, and provides pseudocode implementations. Operations like insert and delete are O(n) linear time due to array element shifting. Search operations can be improved to O(1) constant time using techniques like transposition and move to front. Binary search provides O(logn) time complexity for sorted arrays. The document also discusses sorting an array, checking if an array is sorted, arranging negative numbers to the left, and merging two sorted arrays.
The document discusses heapsort, an efficient sorting algorithm that uses a heap data structure. Heapsort first builds a max heap from the input array in O(n) time using a heapify procedure. It then extracts elements from the heap into the sorted output array, maintaining the heap property, resulting in an overall time complexity of O(n log n).
This document describes the heap sort algorithm. It begins by explaining heaps and their properties like the heap property. It then describes key heap operations like Max-Heapify, Build-Max-Heap, finding the maximum element, extract max, increase key, and insert. These operations allow heaps to function as priority queues. Heap sort works by building a max heap from the input array using Build-Max-Heap, then repeatedly extracting the maximum element and putting it in its sorted position. This runs in O(n log n) time, combining advantages of insertion and merge sort by sorting in-place like insertion sort but with a faster running time of merge sort.
The heap data structure is a nearly complete binary tree implemented as an array. There are two types of heaps: max-heaps and min-heaps. The MAX-HEAPIFY algorithm maintains the heap property by allowing a value to "float down" the tree. BUILD-MAX-HEAP builds a max-heap by calling MAX-HEAPIFY on each node, and HEAPSORT sorts an array using the heap structure.
The document discusses algorithms including heapsort and quicksort. It provides explanations of heap data structures and how they can be used to implement priority queues. It then describes how priority queues can be used to simulate event-driven systems like collisions in a combat billiards simulation. Quicksort is also summarized, including the key partition step that divides the array into two subarrays.
The document discusses priority queues and quicksort. It defines a priority queue as a data structure that maintains a set of elements with associated keys. Heaps can be used to implement priority queues. There are two types: max-priority queues and min-priority queues. Priority queues have applications in job scheduling and event-driven simulation. Quicksort works by partitioning an array around a pivot element and recursively sorting the sub-arrays.
This document provides information about priority queues and binary heaps. It defines a binary heap as a nearly complete binary tree where the root node has the maximum/minimum value. It describes heap operations like insertion, deletion of max/min, and increasing/decreasing keys. The time complexity of these operations is O(log n). Heapsort, which uses a heap data structure, is also covered and has overall time complexity of O(n log n). Binary heaps are often used to implement priority queues and for algorithms like Dijkstra's and Prim's.
The document discusses various sorting algorithms like bubble sort, selection sort, insertion sort, merge sort, quicksort and heap sort. It provides pseudocode to explain the algorithms. For each algorithm, it explains the basic approach, complexity analysis and provides an example to illustrate the steps. Quicksort is explained in more detail with pseudocode and examples to demonstrate how it works by picking a pivot element, partitioning the array and recursively sorting the sub-arrays.
Array ADT(Abstract Data Type)|Data StructureAkash Gaur
The document discusses abstract data types (ADTs) and describes an array ADT. It lists common array operations like display, add, insert, delete, search, get, set, max, min, reverse, shift, and provides pseudocode implementations. Operations like insert and delete are O(n) linear time due to array element shifting. Search operations can be improved to O(1) constant time using techniques like transposition and move to front. Binary search provides O(logn) time complexity for sorted arrays. The document also discusses sorting an array, checking if an array is sorted, arranging negative numbers to the left, and merging two sorted arrays.
The document discusses heapsort, an efficient sorting algorithm that uses a heap data structure. Heapsort first builds a max heap from the input array in O(n) time using a heapify procedure. It then extracts elements from the heap into the sorted output array, maintaining the heap property, resulting in an overall time complexity of O(n log n).
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive function. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
UIU Recruiter, a solution to the recruitments like teaching assistant (ta), grader, research assistant (ra). By the project, workload, hassle, wastage of time can be prevented.
This document discusses entrepreneurship and provides a definition of an entrepreneur as someone who innovates and introduces something new to the economy according to Joseph A. Schumpeter. It also mentions famous entrepreneurs and the financial benefits of entrepreneurship as reasons why one may want to become an entrepreneur and be their own boss.
Android is an open-source operating system used for smartphones and tablets. It was developed by Android Inc which was founded in 2003. Some key events in Android's history include the release of the first commercial Android device, the HTC Dream, in 2008 and Samsung's Galaxy Tab tablet in 2010. Android uses the Linux kernel and runs apps in the Dalvik virtual machine. It has an application framework and core apps. Major Android versions include Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwich, Jelly Bean, KitKat, Lollipop, Marshmallow, Nougat, Oreo and Pie. Features include a beautiful UI, connectivity, storage,
This presentation provides an overview of the history and features of the Android operating system. It discusses how Android was founded in 2003 and the first commercially released devices in 2008. It then summarizes the major Android versions from 1.0 to the upcoming 10.0, and describes the core components of the Android architecture including the Linux kernel, libraries, runtime, and applications. Finally, it outlines some key advantages like customization and hardware choice, as well as disadvantages such as potential security issues.
Embedded machine learning-based road conditions and driving behavior monitoringIJECEIAES
Car accident rates have increased in recent years, resulting in losses in human lives, properties, and other financial costs. An embedded machine learning-based system is developed to address this critical issue. The system can monitor road conditions, detect driving patterns, and identify aggressive driving behaviors. The system is based on neural networks trained on a comprehensive dataset of driving events, driving styles, and road conditions. The system effectively detects potential risks and helps mitigate the frequency and impact of accidents. The primary goal is to ensure the safety of drivers and vehicles. Collecting data involved gathering information on three key road events: normal street and normal drive, speed bumps, circular yellow speed bumps, and three aggressive driving actions: sudden start, sudden stop, and sudden entry. The gathered data is processed and analyzed using a machine learning system designed for limited power and memory devices. The developed system resulted in 91.9% accuracy, 93.6% precision, and 92% recall. The achieved inference time on an Arduino Nano 33 BLE Sense with a 32-bit CPU running at 64 MHz is 34 ms and requires 2.6 kB peak RAM and 139.9 kB program flash memory, making it suitable for resource-constrained embedded systems.
Batteries -Introduction – Types of Batteries – discharging and charging of battery - characteristics of battery –battery rating- various tests on battery- – Primary battery: silver button cell- Secondary battery :Ni-Cd battery-modern battery: lithium ion battery-maintenance of batteries-choices of batteries for electric vehicle applications.
Fuel Cells: Introduction- importance and classification of fuel cells - description, principle, components, applications of fuel cells: H2-O2 fuel cell, alkaline fuel cell, molten carbonate fuel cell and direct methanol fuel cells.
Introduction- e - waste – definition - sources of e-waste– hazardous substances in e-waste - effects of e-waste on environment and human health- need for e-waste management– e-waste handling rules - waste minimization techniques for managing e-waste – recycling of e-waste - disposal treatment methods of e- waste – mechanism of extraction of precious metal from leaching solution-global Scenario of E-waste – E-waste in India- case studies.
The document discusses the benefits of exercise for mental health. Regular physical activity can help reduce anxiety and depression and improve mood and cognitive function. Exercise causes chemical changes in the brain that may help protect against mental illness and improve symptoms.
UIU Recruiter, a solution to the recruitments like teaching assistant (ta), grader, research assistant (ra). By the project, workload, hassle, wastage of time can be prevented.
This document discusses entrepreneurship and provides a definition of an entrepreneur as someone who innovates and introduces something new to the economy according to Joseph A. Schumpeter. It also mentions famous entrepreneurs and the financial benefits of entrepreneurship as reasons why one may want to become an entrepreneur and be their own boss.
Android is an open-source operating system used for smartphones and tablets. It was developed by Android Inc which was founded in 2003. Some key events in Android's history include the release of the first commercial Android device, the HTC Dream, in 2008 and Samsung's Galaxy Tab tablet in 2010. Android uses the Linux kernel and runs apps in the Dalvik virtual machine. It has an application framework and core apps. Major Android versions include Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice Cream Sandwich, Jelly Bean, KitKat, Lollipop, Marshmallow, Nougat, Oreo and Pie. Features include a beautiful UI, connectivity, storage,
This presentation provides an overview of the history and features of the Android operating system. It discusses how Android was founded in 2003 and the first commercially released devices in 2008. It then summarizes the major Android versions from 1.0 to the upcoming 10.0, and describes the core components of the Android architecture including the Linux kernel, libraries, runtime, and applications. Finally, it outlines some key advantages like customization and hardware choice, as well as disadvantages such as potential security issues.
Embedded machine learning-based road conditions and driving behavior monitoringIJECEIAES
Car accident rates have increased in recent years, resulting in losses in human lives, properties, and other financial costs. An embedded machine learning-based system is developed to address this critical issue. The system can monitor road conditions, detect driving patterns, and identify aggressive driving behaviors. The system is based on neural networks trained on a comprehensive dataset of driving events, driving styles, and road conditions. The system effectively detects potential risks and helps mitigate the frequency and impact of accidents. The primary goal is to ensure the safety of drivers and vehicles. Collecting data involved gathering information on three key road events: normal street and normal drive, speed bumps, circular yellow speed bumps, and three aggressive driving actions: sudden start, sudden stop, and sudden entry. The gathered data is processed and analyzed using a machine learning system designed for limited power and memory devices. The developed system resulted in 91.9% accuracy, 93.6% precision, and 92% recall. The achieved inference time on an Arduino Nano 33 BLE Sense with a 32-bit CPU running at 64 MHz is 34 ms and requires 2.6 kB peak RAM and 139.9 kB program flash memory, making it suitable for resource-constrained embedded systems.
Batteries -Introduction – Types of Batteries – discharging and charging of battery - characteristics of battery –battery rating- various tests on battery- – Primary battery: silver button cell- Secondary battery :Ni-Cd battery-modern battery: lithium ion battery-maintenance of batteries-choices of batteries for electric vehicle applications.
Fuel Cells: Introduction- importance and classification of fuel cells - description, principle, components, applications of fuel cells: H2-O2 fuel cell, alkaline fuel cell, molten carbonate fuel cell and direct methanol fuel cells.
Introduction- e - waste – definition - sources of e-waste– hazardous substances in e-waste - effects of e-waste on environment and human health- need for e-waste management– e-waste handling rules - waste minimization techniques for managing e-waste – recycling of e-waste - disposal treatment methods of e- waste – mechanism of extraction of precious metal from leaching solution-global Scenario of E-waste – E-waste in India- case studies.
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...IJECEIAES
Medical image analysis has witnessed significant advancements with deep learning techniques. In the domain of brain tumor segmentation, the ability to
precisely delineate tumor boundaries from magnetic resonance imaging (MRI)
scans holds profound implications for diagnosis. This study presents an ensemble convolutional neural network (CNN) with transfer learning, integrating
the state-of-the-art Deeplabv3+ architecture with the ResNet18 backbone. The
model is rigorously trained and evaluated, exhibiting remarkable performance
metrics, including an impressive global accuracy of 99.286%, a high-class accuracy of 82.191%, a mean intersection over union (IoU) of 79.900%, a weighted
IoU of 98.620%, and a Boundary F1 (BF) score of 83.303%. Notably, a detailed comparative analysis with existing methods showcases the superiority of
our proposed model. These findings underscore the model’s competence in precise brain tumor localization, underscoring its potential to revolutionize medical
image analysis and enhance healthcare outcomes. This research paves the way
for future exploration and optimization of advanced CNN models in medical
imaging, emphasizing addressing false positives and resource efficiency.
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)
}
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…
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.