Mastering Heap Sort: A Comprehensive Guide with Implementation in C

Mastering Heap Sort: A Comprehensive Guide with Implementation in C

orting algorithms are integral to computer science, enabling efficient data organization and retrieval. One such powerful algorithm is Heap Sort, which leverages the properties of binary heaps. In this blog, we’ll explore Heap Sort’s mechanism, its advantages, and provide a C implementation for better understanding.


What is Heap Sort?

Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure to organize and sort elements. It is an in-place, non-stable algorithm with a worst-case time complexity of , making it efficient for large datasets.

Key Concepts:

  • Binary Heap: A complete binary tree where:

    • In a max-heap, every parent node is greater than or equal to its child nodes.

    • In a min-heap, every parent node is less than or equal to its child nodes.

  • Heap Sort typically uses a max-heap to sort elements in ascending order.

How Does Heap Sort Work?

Steps of the Algorithm:

  1. Build a Max-Heap: Rearrange the input array into a max-heap.

  2. Extract Elements:

    • Swap the root (maximum element) with the last element of the heap.

    • Reduce the heap size by one and heapify the root to restore the heap property.

  3. Repeat until all elements are sorted.

C Implementation of Heap Sort

Here’s a simple and efficient implementation of Heap Sort in C:

#include <stdio.h>

// Function to heapify a subtree rooted at node i
a

    int largest = i;
    int left = 2 * i + 1; // Left child
    int right = 2 * i + 2; // Right child

    // Check if left child is larger than root
    if (left < n && arr[left] > arr[largest])
        largest = left;

    // Check if right child is larger than the largest so far
    if (right < n && arr[right] > arr[largest])
        largest = right;

    // Swap and continue heapifying if root is not largest
    if (largest != i) {
        int temp = arr[i];
        arr[i] = arr[largest];
        arr[largest] = temp;

        // Recursively heapify the affected subtree
        heapify(arr, n, largest);
    }
}

// Main function to implement Heap Sort
void heapSort(int arr[], int n) {
    // Build a max-heap
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    // Extract elements from the heap one by one
    for (int i = n - 1; i > 0; i--) {
        // Move current root to the end
        int temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;

        // Call heapify on the reduced heap
        heapify(arr, i, 0);
    }
}

// Utility function to print an array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: \n");
    printArray(arr, n);

    heapSort(arr, n);

    printf("\nSorted array: \n");
    printArray(arr, n);

    return 0;
}

Explanation of the Code

  1. Heapify Function:

    • Ensures the subtree rooted at a given index satisfies the max-heap property.

    • Recursively adjusts the heap if the root is not the largest.

  2. Heap Sort Function:

    • Builds a max-heap from the input array.

    • Repeatedly swaps the root with the last element and reduces the heap size, maintaining the heap property after each extraction.

  3. Main Function:

    • Demonstrates the usage of the heapSort function with a sample array.

Advantages of Heap Sort

  • Efficiency: Time complexity of for all cases.

  • In-Place Sorting: Requires no additional memory for sorting.

  • Deterministic: Consistent performance regardless of the input order.

Disadvantages of Heap Sort

  • Non-Stable: Relative order of equal elements may change.

  • Cache Performance: May not perform as well as Quick Sort for smaller datasets due to cache inefficiency.

Conclusion

Heap Sort is a robust and efficient sorting algorithm that stands out for its reliability and in-place sorting capability. While it may not always outperform other sorting methods like Quick Sort for smaller datasets, its predictable complexity makes it a solid choice for larger datasets.