C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Heap Sort

Concept Overview

Heap Sort works by:

  1. Building a Max Heap (for ascending order).
  2. Repeatedly extracting the maximum element (root) and placing it at the end of the array.
  3. Reducing the heap size and re-heapifying until the entire array is sorted.

Binary Heap Basics

A Max Heap satisfies two properties:

  1. It’s a complete binary tree (filled except maybe last level).
  2. Each node’s value is greater than or equal to its children.

Heap represented as an array:

Index: 0 1 2 3 4 5

Heap: [50, 30, 40, 10, 20, 35]

Relationships:

  • Left child = 2*i + 1
  • Right child = 2*i + 2
  • Parent = (i - 1) / 2

 

C Program: Heap Sort

C

#include <stdio.h>

 

// Function to heapify a subtree rooted with node i (0-based index)

void heapify(int arr[], int n, int i) {

    int largest = i;        // Initialize largest as root

    int left = 2 * i + 1;   // left child index

    int right = 2 * i + 2;  // right child index

 

    // If left child is larger than root

    if (left < n && arr[left] > arr[largest])

        largest = left;

 

    // If right child is larger than largest so far

    if (right < n && arr[right] > arr[largest])

        largest = right;

 

    // If largest is not root

    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 perform Heap Sort

void heapSort(int arr[], int n) {

    // Step 1: Build a max heap

    for (int i = n / 2 - 1; i >= 0; i--)

        heapify(arr, n, i);

 

    // Step 2: Extract elements one by one

    for (int i = n - 1; i > 0; i--) {

        // Move current root to end

        int temp = arr[0];

        arr[0] = arr[i];

        arr[i] = temp;

 

        // Heapify reduced heap

        heapify(arr, i, 0);

    }

}

 

// Function to print array

void printArray(int arr[], int n) {

    for (int i = 0; i < n; i++)

        printf("%d ", arr[i]);

    printf("\n");

}

 

int main() {

    int n;

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    int arr[n];

    printf("Enter %d elements:\n", n);

    for (int i = 0; i < n; i++)

        scanf("%d", &arr[i]);

 

    printf("Original array: ");

    printArray(arr, n);

 

    heapSort(arr, n);

 

    printf("Sorted array: ");

    printArray(arr, n);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 6
Enter 6 elements:
50 30 40 10 20 35

OUTPUT :
Original array: 50 30 40 10 20 35 
Sorted array: 10 20 30 35 40 50

Algorithm Summary

Step

Description

1

Build a Max Heap from input array

2

Swap root (max) with last element

3

Reduce heap size by 1

4

Heapify the root again

5

Repeat until array is sorted

Time and Space Complexity

Case

Time Complexity

Space Complexity

Best Case

O(n log n)

O(1)

Average Case

O(n log n)

O(1)

Worst Case

O(n log n)

O(1)

  • In-place algorithm — no extra array needed.
  • Consistent O(n log n)
  • Not stable (equal elements may change order).

Key Takeaways

  • Uses binary heap
  • Efficient and in-place.
  • Performance independent of input order.
  • Commonly used in priority queues.
  • Not suitable when stability is required.

 

Comparison with Other Sorts

Algorithm

Time Complexity

Space

Stable

Approach

Bubble Sort

O(n²)

O(1)

Comparison

Insertion Sort

O(n²)

O(1)

Incremental

Quick Sort

O(n log n)

O(log n)

Divide & Conquer

Merge Sort

O(n log n)

O(n)

Divide & Conquer

Heap Sort

O(n log n)

O(1)

Selection-based