2

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;
}

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's fine, although you're doing more work than necessary. You can start the first loop at (q-1)/2, because the items below that are at the leaf level and can't possibly move down.
1

You have several problems in your code

printA

One is/can be indicated by the compiler, in printA :

printf("%d\n");

‘%d’ expects a matching ‘int’ argument, but there no no argument

It is easy to guess you just wanted to print a newline, so that line can be replaced by

putchar('\n');

Still in printA you print the numbers without a separator, the result is not usable, for instance do

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

When I look at the call of printA in main the parameter n is the number of elements in A, so the end test of the for is invalid because you try to print a value out of the array, the loop must be :

for( i = 0; i < q; i++)

max_heap_append

in the second for the index i can value 0, in that case you swap the first element of the array with itself, that has no sense and the same for the call of heapify with the 2 last arguments valuing 0

When you call that function in main the parameter q receive the number of elements in the array, which is also the first value of i still in that second for and &A[i] is out of the array. You need to replace that line by

for( i = q-1; i> 0; i--)

If I do all these changes :

Compilation and execution :

bruno@bruno-XPS-8300:/tmp$ gcc -g -Wall h.c
bruno@bruno-XPS-8300:/tmp$ ./a.out
Sorted: 2 3 8 9 10 11 12 14 
bruno@bruno-XPS-8300:/tmp$ 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.