heap sort algorithm program using C language

 #include <stdio.h>


// Function to print tree structure from array

void printTree(int a[], int n) {

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

        int left = 2 * i + 1;

        int right = 2 * i + 2;


        printf("\nParent: %d", a[i]);


        if (left < n)

            printf(" | Left: %d", a[left]);

        else

            printf(" | Left: NULL");


        if (right < n)

            printf(" | Right: %d", a[right]);

        else

            printf(" | Right: NULL");

    }

    printf("\n-------------------------\n");

}


// Function to heapify a subtree rooted at index i

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

    int largest = i;          // Root index

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

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


    // If left child is larger than root

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

        largest = left;


    // If right child is larger than largest so far

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

        largest = right;


    // If largest is not root

    if (largest != i) {

        int temp = a[i];

        a[i] = a[largest];

        a[largest] = temp;


        // Recursively heapify the affected subtree

        heapify(a, n, largest);

    }

}


// Heap Sort function

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

    // Build heap (rearrange array)

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

        heapify(a, n, i);


    printf("Max Heap built:\n");

    printTree(a, n);


    // One by one extract elements

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

        // Swap root (max) with last element

        int temp = a[0];

        a[0] = a[i];

        a[i] = temp;


        printf("After moving max %d to end:\n", a[i]);

        printTree(a, i); // Print remaining heap


        // Heapify reduced heap

        heapify(a, i, 0);

    }

}


int main() {

    int a[] = {1, 12, 3, 4,10,15};

    int n = sizeof(a) / sizeof(a[0]);


    printf("Original array:\n");

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

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

   

 printTree(a, n);


    heapSort(a, n);


    printf("Sorted array: ");

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

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

    printf("\n");


    return 0;

}




Output:-

Original array:


Parent: 1 | Left: 12 | Right: 3

Parent: 12 | Left: 4 | Right: 10

Parent: 3 | Left: 15 | Right: NULL

Parent: 4 | Left: NULL | Right: NULL

Parent: 10 | Left: NULL | Right: NULL

Parent: 15 | Left: NULL | Right: NULL

-------------------------

Max Heap built:


Parent: 15 | Left: 12 | Right: 3

Parent: 12 | Left: 4 | Right: 10

Parent: 3 | Left: 1 | Right: NULL

Parent: 4 | Left: NULL | Right: NULL

Parent: 10 | Left: NULL | Right: NULL

Parent: 1 | Left: NULL | Right: NULL

-------------------------

After moving max 15 to end:


Parent: 1 | Left: 12 | Right: 3

Parent: 12 | Left: 4 | Right: 10

Parent: 3 | Left: NULL | Right: NULL

Parent: 4 | Left: NULL | Right: NULL

Parent: 10 | Left: NULL | Right: NULL

-------------------------

After moving max 12 to end:


Parent: 1 | Left: 10 | Right: 3

Parent: 10 | Left: 4 | Right: NULL

Parent: 3 | Left: NULL | Right: NULL

Parent: 4 | Left: NULL | Right: NULL

-------------------------

After moving max 10 to end:


Parent: 1 | Left: 4 | Right: 3

Parent: 4 | Left: NULL | Right: NULL

Parent: 3 | Left: NULL | Right: NULL

-------------------------

After moving max 4 to end:


Parent: 3 | Left: 1 | Right: NULL

Parent: 1 | Left: NULL | Right: NULL

-------------------------

After moving max 3 to end:


Parent: 1 | Left: NULL | Right: NULL

-------------------------

Sorted array: 1 3 4 10 12 15 



Comments

Popular posts from this blog

B.tech 5th Sem Machin learning lab practical

BCA 3rd year Web Technology(code 304) Web Site development using ASP.NET AND C#

DWDM 2024 question paper with solution