Skip to content
Home Ā» Heap sort

Heap sort


šŸ“˜ 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

  1. Build a Max Heap from the array
  2. Swap root (largest) with last element
  3. Reduce heap size
  4. Apply heapify to restore heap
  5. 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

CaseComplexity
Best CaseO(n log n)
Average CaseO(n log n)
Worst CaseO(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

AlgorithmTime ComplexitySpace
Heap SortO(n log n)O(1)
Quick SortO(n log n) avgO(log n)
Merge SortO(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