#include<stdio.h>
#include<conio.h>

int n,a[10];
void display();
void insert(int);
int del();
int main()
{
    int i,item=0,j;
    n=1;
     printf("enter the numbers:");
    for(i=1;i<6;i++)
    scanf("%d",&a[i]);
    for(j=1;j<5;j++)
    {
                insert(a[j+1]);
     }     }
     while(n>1)
     {
               item=del();
               a[n+1]=item;
     }
     display(a);
     getch();
}
void display()
{
     int i;
     for(i=1;i<6;i++)
     {
                     printf("%d ",a[i]);
      }
     
 }
 
void insert(int item1)
{  
     int ptr,par;
      n++;
     ptr=n;
    
     while(ptr>1)
     {
                 par=ptr/2;
                 if(item1<a[par])
                 {
                                    a[ptr]=item1;
                                    display(a);
                                    return;
                 }
                 a[ptr]=a[par];
                 ptr=par;
     }
     a[1]=item1;
     display(a);
}
  
int del()
{
    int item,left,right,ptr,last;
    item=a[1];
    last=a[n];
    n--;
    ptr=1;
    left=2;
    right=3;
    while(right<=n)
    {
                   if(last>a[left] && last>a[right])
                   {
                                       a[ptr]=last;
                                       return(item);
                   }
                  if(a[left]>a[right])
                   {
                                              a[ptr]=a[left];
                                              ptr=left;
                   }
                   else
                   {
                       a[ptr]=a[right];
                       ptr=right;
                   }
                   left=ptr*2;
                   right=left+1;
    }
    if(left==n && last<a[left])
    {          
               ptr=left;
               a[ptr]=last;
    }
    return(item);
}

please tell me why is it not working..? there is no syntax error. and insertion is going very well. but facing problem when deletion is there. any help will be appreciated.

thanx.
(m just a beginner)

Recommended Answers

All 6 Replies

Please post code inside code blocks with proper indentation of branches and loops so we can read it easier. Thanks.

Like this:

#include<stdio.h>
#include<conio.h>

int n, a[10];

void display();

void insert(int);

int del();

int main()
{
    int i, item = 0, j;
    n = 1;
    printf("enter the numbers:");
    for(i = 1; i < 6; i++)
        scanf("%d", &a[i]);
    for(j = 1; j < 5; j++)
    {
        insert(a[j+1]);
    }
    display();
    while(n > 1)
    {
        item = del();
        a[n+1] = item;

    }
    display();
    getch();
}

void display()
{
    int i;
    for(i = 1; i < 6; i++)
    {
        printf("%d ", a[i]);
    }

}

void insert(int item1)
{
    int ptr, par;
    n++;
    ptr = n;

    while(ptr > 1)
    {
        par = ptr / 2;
        if(item1 < a[par])
        {
            a[ptr] = item1;

            return;
        }
        a[ptr] = a[par];
        ptr = par;
    }
    a[1] = item1;

}

int del()
{
    int item, left, right, ptr, last;
    item = a[1];
    last = a[n];
    n--;
    ptr = 1;
    left = 2;
    right = 3;
    while(right <= n)
    {
        if(last > a[left] && last > a[right])
        {
            a[ptr] = last;
            return(item);
        }
        if(a[left] > a[right])
        {
            a[ptr] = a[left];
            ptr = left;
        }
        else
        {
            a[ptr] = a[right];
            ptr = right;
        }
        left = ptr * 2;
        right = left + 1;
    }
    if(left == n && last < a[left])
    {
        ptr = left;
        a[ptr] = last;
    }
    return(item);
}

Like this:

#include<stdio.h>
#include<conio.h>

int n, a[10];

void display();

void insert(int);

int del();

int main()
{
    int i, item = 0, j;
    n = 1;
    printf("enter the numbers:");
    for(i = 1; i < 6; i++)
        scanf("%d", &a[i]);
    for(j = 1; j < 5; j++)
    {
        insert(a[j+1]);
    }
    display();
    while(n > 1)
    {
        item = del();
        a[n+1] = item;

    }
    display();
    getch();
}

void display()
{
    int i;
    for(i = 1; i < 6; i++)
    {
        printf("%d ", a[i]);
    }

}

void insert(int item1)
{
    int ptr, par;
    n++;
    ptr = n;

    while(ptr > 1)
    {
        par = ptr / 2;
        if(item1 < a[par])
        {
            a[ptr] = item1;

            return;
        }
        a[ptr] = a[par];
        ptr = par;
    }
    a[1] = item1;

}

int del()
{
    int item, left, right, ptr, last;
    item = a[1];
    last = a[n];
    n--;
    ptr = 1;
    left = 2;
    right = 3;
    while(right <= n)
    {
        if(last > a[left] && last > a[right])
        {
            a[ptr] = last;
            return(item);
        }
        if(a[left] > a[right])
        {
            a[ptr] = a[left];
            ptr = left;
        }
        else
        {
            a[ptr] = a[right];
            ptr = right;
        }
        left = ptr * 2;
        right = left + 1;
    }
    if(left == n && last < a[left])
    {
        ptr = left;
        a[ptr] = last;
    }
    return(item);
}

ohh. !! really thnxx.. ok now can u plzz solve my doubt.? finf error in that error is in deletion...

I ran your program with an input of 2, 5, 3, 6, 4 . The heap is 6, 5, 3, 2, 4 , which looks okay, but after it does the 'sort' part of the heap sort, I end up with 5, 5, 5, 5, 6 .

It looks like you're not swapping array members correctly--it's as if one of them is getting copied and the other lost. As a first step, I recommend you write a swap function, something like void swap(int a, int b) { ... } , that exchanges the two values at indexes a and b within your array. Then rewrite the rest of your code to use that function instead of doing it directly.

Doing this will make your code easier to read, and it might expose an obvious problem. If it still doesn't work, we can look at your heap sort implementation in more detail.

I ran your program with an input of 2, 5, 3, 6, 4 . The heap is 6, 5, 3, 2, 4 , which looks okay, but after it does the 'sort' part of the heap sort, I end up with 5, 5, 5, 5, 6 .

It looks like you're not swapping array members correctly--it's as if one of them is getting copied and the other lost. As a first step, I recommend you write a swap function, something like void swap(int a, int b) { ... } , that exchanges the two values at indexes a and b within your array. Then rewrite the rest of your code to use that function instead of doing it directly.

Doing this will make your code easier to read, and it might expose an obvious problem. If it still doesn't work, we can look at your heap sort implementation in more detail.

no! man it is still not working. m upset now. :'(

no! man it is still not working. m upset now. :'(

Post your new code, please. Also, what does "not working" mean here? Are you getting the same output as before, or is it going wrong differently after the change?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.