0% found this document useful (0 votes)
4 views8 pages

Introduction To Heap Sort

Heap Sort is a comparison-based sorting algorithm that uses the heap data structure, achieving O(n log n) time complexity in all cases while sorting in-place with constant extra space. It involves building a max heap and repeatedly extracting the maximum element to sort the array. The algorithm is efficient and has applications in priority queues and embedded systems.

Uploaded by

lennylewis6a6y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Introduction To Heap Sort

Heap Sort is a comparison-based sorting algorithm that uses the heap data structure, achieving O(n log n) time complexity in all cases while sorting in-place with constant extra space. It involves building a max heap and repeatedly extracting the maximum element to sort the array. The algorithm is efficient and has applications in priority queues and embedded systems.

Uploaded by

lennylewis6a6y
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction to Heap Sort

Heap Sort is a comparison-based sorting algorithm. It is part of the


selection sort family. This algorithm utilizes the heap data structure
for efficient sorting. It guarantees O(n log n) time complexity in all
cases. Also, it sorts in-place, requiring only constant extra space.

by Lenny Lewis
Understanding Heaps
Binary Heap Max Heap Array Representation

A binary heap is a complete binary The root node is always the largest For a node at index `i`, the left
tree. It can be represented as an element. Every node's value is child is `2*i + 1`, the right child is
array. Two types: Max Heap and greater than or equal to its `2*i + 2`, and the parent is `(i - 1) /
Min Heap. children's values. 2`.
Building the Max Heap
Heapify
Maintains the max heap property. Start from the last non-
leaf node. Compare the node with its children.

Swap
If the node is smaller than either child, swap it with the
larger child.

Recursively Heapify
Recursively heapify the affected sub-tree.

Build Heap
Start from the middle of the array and go backwards. For
each node, call heapify.
Heap Sort: Sorting Phase

2 Reduce Heap Size

1
Swap Root

Heapify Root
3

Repeatedly extract the maximum element from the heap. Then place it at the end of the sorted array.

Swap the root with the last element. Reduce heap size. Heapify the root. Repeat until size is 1.
Detailed Example (Part 1)

1 Unsorted Array 2 Build Max Heap


[4, 1, 3, 2, 16, 9, 10, 14, 8, 7] Start with 16 and 9.
Heapify to get a valid
max heap.

3 Resulting Heap
[16, 14, 10, 8, 7, 9, 3, 2, 4, 1]
Detailed Example (Part 2)

Sorted Array
Resulting Heap
Heapify Root
Swap 16 with 7

Continue sorting. Swap 16 with 7: [7, 14, 10, 8, 4, 9, 3, 2, 4, 16]. Heapify the root node (7). The sorted portion grows.
Complexity Analysis
Building Heap O(n)

Sorting O(n log n)

Overall O(n log n)

Space O(1)

Building the heap takes O(n) time. Sorting takes O(n log n) time.
The overall time complexity is O(n log n). It sorts in place with O(1)
space complexity.
Applications and
Conclusion

Priority Queues Embedded Guaranteed


Systems Performance

Heap Sort is efficient. It's a comparison-based algorithm with


guaranteed O(n log n) time complexity and sorts in-place.
Understanding different sorting algorithms is important.

You might also like