Search code examples
algorithmdata-structuresheapheapsortmax-heap

Using heap sort, append an array elements


I have given an array int A[] = {12,10,9,2,11,8,14,3,5}; In this array, 1st 4 elements(from index 0 to index 3) follow max heap condition. But last 5 elements(index 4 to index 8) don't follow max heap condition. So, I have to write a code so that the whole array follow max heap condition.

I have given a function call max_heap_append(A,3,8); and I have to use it in my code to write the program. It is an assignment so I have to follow the instruction.

I have written this code bellow but when I run the program, nothing happens.

#include <stdio.h>
#include <stdlib.h>

void swap(int * a, int * b )
{
    int temp;

    temp = *a;
    *a = *b;
    *b = temp;

}

void heapify( int A[], int q, int i) 
{
    int largest = i;
    int l = 2 * i + 1 ;
    int r = 2 * i + 2;

    if( l < q && A[l] > A[largest])
    {
        largest = l;
    }
    if( r < q && A[r] > A[largest])
    {
        largest = r;
    }
    if( largest != i)
    {
        swap( &A[i] , &A[largest]);
        heapify(A, q, largest);
    }
}



void max_heap_append(int A[], int p , int q) 
{
    int i;

    for( i = q / 2 -1; i >= 0; i--)  
    {
        heapify( A , q , i);
    }
    // sort the heap
    for( i = q; i>= 0; i--)
    {
        swap(&A[0] , &A[i]);

        heapify(A, i, 0);
    }


}
 void printA(int A[], int q)
 {
     int i;
     for( i = 0; i <= q; i++)
     {
         printf("%d", A[i]);

     }
     printf("%d\n");
 }


int main()
{

    int A[] = {12,10,9,2,11,8,14,3};

    max_heap_append(A,3,8);

    printf("Sorted: ");

    printA(A, 8);

    return 0;
}

Solution

  • Its not followed heapify from 0 to 3 index.. so u need to heapify all. there is some mistake. if your array size is 8 then u can not excess a[8], you can access a[0] to a[7]. so you need to iterate from 0 to 7.

    Try with this:

    #include <stdio.h>
    #include <stdlib.h>
    
    void swap(int * a, int * b )
    {
        int temp;
    
        temp = *a;
        *a = *b;
        *b = temp;
    
    }
    
    void heapify( int A[], int q, int i)
    {
        int largest = i;
        int l = 2 * i + 1 ;
        int r = 2 * i + 2;
    
        if( l < q && A[l] > A[largest])
        {
            largest = l;
        }
        if( r < q && A[r] > A[largest])
        {
            largest = r;
        }
        if( largest != i)
        {
            swap( &A[i] , &A[largest]);
            heapify(A, q, largest);
        }
    }
    
    
    
    void max_heap_append(int A[], int p , int q)
    {
        int i;
    
        for( i = q-1; i >= 0; i--)
        {
            heapify( A , q , i);
        }
        // sort the heap
        for( i = q-1; i>= 0; i--)
        {
            swap(&A[0] , &A[i]);
    
            heapify(A, i, 0);
        }
    
    
    }
    void printA(int A[], int q)
    {
        int i;
        for( i = 0; i < q; i++)
        {
            printf("%d ", A[i]);
    
        }
        printf("\n");
    }
    
    
    int main()
    {
    
        int A[] = {12,10,9,2,11,8,14,3};
    
        max_heap_append(A,3,8);
    
        printf("Sorted: ");
    
        printA(A, 8);
    
        return 0;
    }