Heap Sort
Heap Sort
Topic:HEAP
-SORT
Once the max heap is created, the largest element is swapped with
the last element, reducing the heap size.
Array a[].
a[0]
How does Heap Sort Works? 10
Example a[2]
Step 2- The heapify process is a[1] 15 5
crucial for maintaining the heap
structure. It involves adjusting the
positions of elements to ensure the 8
a[3]
heap property is preserved after a
swap.
[10 and 15 will be swapped to preserve
the property of max heap]
Array a[].
a[0]
How does Heap Sort Works? 15
Example a[2]
Step 3- same steps will be repeated a[1] 10 5
till we get the sorted array a[].
8
[swapping of 8 and 15 reducing the a[3]
heap size
Array a[].
How does Heap Sort Works?
Example-
a[0] a[0]
heapify
8 10
a[2] a[2]
a[1] 10 5 a[1] 8 5
a[1] 8 a[1] 5
Sorted
array a[].
Array a[].
Complexity
Heap Sort has a Time complexity of O(n log n) in
the average and worst cases.
The building of the heap takes O(n) time, while the
sorting process requires O(log n) time for each
element.
This makes Heap Sort efficient for large datasets.
Its Space complexity of O(1). This means it sorts
the elements in place without needing additional
storage for another array.
Disadvantages of Heap Sort
Despite its strengths, Heap Sort has drawbacks,
such as being not stable, which means that equal
elements may not retain their original order.
Additionally, its constant factors can make it
slower than other algorithms like quick-sort for
smaller datasets.
THANK YOU