微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我已经写了一个代码 create max heap direct from array into a created a dummy array 但是输出是错误的,有人能指出错误吗

如何解决我已经写了一个代码 create max heap direct from array into a created a dummy array 但是输出是错误的,有人能指出错误吗

int swap(int *dum,int i,int temp)
{
    int j=*(dum+temp);
    
    *(dum+temp)=*(dum+i);// swapping numbers in index i and temp of array dum
    
    *(dum+i)=j;
    
     return 0;
}

int main()
{
    int a[6]={32,40,15,50,5,44};
    
    int n=6;
    
    int *dum=new int[n];
    
    for(int i=0;i<n;i++)
    {
        *(dum+i)=*(a+i);// assigning input array elements to a dummy error 
        
        if(i>0)
        {
            int temp=(i-1)/2;
            
            while(*(dum+i)>*(dum+temp))// checking if a particular node is larger than parent node data 
            {
                swap(dum,i,temp);// calling a function to swap the data a indices i and temp
                
                 i=temp;// i will Now hold the index of parent of prevIoUs i
                
                temp=(i-1)/2;//temp will Now hold parent of new i
             
                if(i==0)
                {
                    break;
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
          cout<<*(dum+i)<<"\n";//print data members in dummy array
    }
}

OUTPUT:// 这是输出,旁边是想要的输出

50 //50
50 //40
44 //44
50 //32
5 //15
44 //5

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。