š Heap Sort (with Running Time Analysis)
1ļøā£ Introduction
Heap Sort is an efficient comparison-based sorting algorithm based on the Binary Heap data structure.
š¹ Idea
- Convert the array into a Max Heap
- The largest element is at the root
- Swap root with last element
- Reduce heap size and repeat
It combines the benefits of selection sort + heap structure.
2ļøā£ What is a Heap?
š¹ Binary Heap
A complete binary tree satisfying:
- Max Heap ā Parent ā„ Children
- Min Heap ā Parent ⤠Children
š Heap is usually represented using an array
3ļøā£ Working Principle
š¹ Steps
- Build a Max Heap from the array
- Swap root (largest) with last element
- Reduce heap size
- Apply heapify to restore heap
- Repeat until sorted
4ļøā£ Algorithm (Heap Sort)
š¹ Pseudocode
HeapSort(A, n):
buildMaxHeap(A, n)
for i = n-1 down to 1:
swap A[0] and A[i]
heapify(A, 0, i)
š¹ Heapify Function
heapify(A, i, n):
largest = i
left = 2*i + 1
right = 2*i + 2
if left < n and A[left] > A[largest]:
largest = left
if right < n and A[right] > A[largest]:
largest = right
if largest != i:
swap A[i] and A[largest]
heapify(A, largest, n)
5ļøā£ Example (Step-by-Step)
š¹ Given Array:
A = [4, 10, 3, 5, 1]
š¹ Step 1: Build Max Heap
[10, 5, 3, 4, 1]
š¹ Step 2: Swap root with last
[1, 5, 3, 4, 10]
Heapify:
[5, 4, 3, 1, 10]
š¹ Step 3: Repeat
[4, 1, 3, 5, 10]
ā [3, 1, 4, 5, 10]
ā [1, 3, 4, 5, 10]
ā Final Sorted Array:
[1, 3, 4, 5, 10]
6ļøā£ Running Time Analysis
š¹ Step 1: Build Heap
- Time = O(n)
š¹ Step 2: Heapify for each element
- Heapify takes O(log n)
- Done n times
[
Total = O(n \log n)
]
7ļøā£ Time Complexity
| Case | Complexity |
|---|---|
| Best Case | O(n log n) |
| Average Case | O(n log n) |
| Worst Case | O(n log n) |
ā Same in all cases
8ļøā£ Space Complexity
- O(1) (in-place)
- No extra memory required
9ļøā£ Recursion Tree Insight
- Height of heap = log n
- Each heapify operation = log n
- Repeated n times ā n log n
š Characteristics of Heap Sort
ā In-place sorting
ā Not stable
ā Uses heap data structure
ā Guaranteed O(n log n)
1ļøā£1ļøā£ Advantages
- No extra memory required
- Guaranteed performance
- Suitable for large datasets
- Better than O(n²) algorithms
1ļøā£2ļøā£ Disadvantages
- Not stable
- Slower than Quick Sort in practice
- More complex implementation
1ļøā£3ļøā£ Comparison with Other Sorting Algorithms
| Algorithm | Time Complexity | Space |
|---|---|---|
| Heap Sort | O(n log n) | O(1) |
| Quick Sort | O(n log n) avg | O(log n) |
| Merge Sort | O(n log n) | O(n) |
š Conclusion
Heap Sort is a powerful and efficient sorting algorithm that guarantees O(n log n) performance with no extra memory.
It uses the heap data structure to repeatedly extract the maximum element.
š Exam Tip
š Always include:
- Heap definition
- Heapify process
- Time complexity
- Example
