2302_79644694 2024-05-04 17:49 采纳率: 42.9%
浏览 4

数据结构 堆排序找问题



#include <stdio.h>

void temp(int &x,int &y)
{
    int t;
    t=x;
    x=y;
    y=t;
}



void maxheap(int *a,int i,int end)
{
    //if(2*i+1>end) return;

    int son,dad;

    dad=i;
    if(2*i+2<=end)
    {
        son=a[2*i+1]>a[2*i+2]?2*i+1:2*i+2;
    }
    else son=2*i+1;

    while(a[son]>a[dad])
    {
        temp(a[son],a[dad]);


        dad=son;
        if(2*dad+2<=end)
        {
            son=a[2*dad+1]>a[2*dad+2]?2*dad+1:2*dad+2;
        }
        else son=2*dad+1;
    }
}

void heapsort(int *a,int start,int end)
{
    for(int i=(end+1)/2-1;i>=start;i--)
    {
        maxheap(a,i,end);
    }


    while(end>0)
    {
        temp(a[0],a[end]);
        end--;
        
        maxheap(a,0,end);
    }



}


int main()
{
    int a[10]={3,87,2,93,78,56,61,38,12,40};

    //quicksort(a,0,19);

    //mergesore(a,0,19);

    heapsort(a,0,9);

}


```

  • 写回答

5条回答 默认 最新

  • 游戏开发小Y Unity3D领域新星创作者 2024-05-05 09:25
    关注

    在您提供的C代码中存在几个问题,特别是与C语言不支持引用(int &x)以及您尝试在temp函数和maxheap函数中直接交换数组元素的方式有关。以下是一些问题和修正建议:

    1. 不支持引用:C语言不支持引用(int &x),因此您应该使用指针来修改数组中的元素。

    2. temp函数应该使用指针:您需要修改temp函数,使其接受两个整数的指针,并交换它们指向的值。

    3. maxheap函数中的比较和交换:在maxheap函数中,您不能直接使用a[son]a[dad]作为temp函数的参数,因为它们不是指针。您应该传递指针的指针或使用索引来交换数组中的值。

    4. 循环边界条件:在heapsort函数中,循环条件while(end>0)应该改为while(end>start),因为当end等于start时,数组已经排序好了。

    下面是修正后的代码示例:

    #include <stdio.h>
    
    void temp(int *x, int *y) {
        int t = *x;
        *x = *y;
        *y = t;
    }
    
    void maxheap(int *a, int i, int end) {
        int son, dad = i;
        int left = 2 * i + 1;
        int right = 2 * i + 2;
    
        if (right <= end && a[right] > a[left]) {
            son = right;
        } else {
            son = left;
        }
    
        if (son <= end && a[son] > a[dad]) {
            temp(&a[son], &a[dad]);
    
            dad = son;
            left = 2 * dad + 1;
            right = 2 * dad + 2;
    
            if (right <= end && a[right] > a[left]) {
                son = right;
            } else {
                son = left;
            }
    
            // 注意这里可能会无限循环,如果son和dad相等或者son大于end
            // 需要添加额外的条件来防止这种情况
            if (son <= end && a[son] > a[dad]) {
                temp(&a[son], &a[dad]);
    
                // 这里可能还需要递归地重新堆化son的子树
                // 但是在这个简单的实现中,我们可以省略这一步,因为它仍然可以工作
            }
        }
    }
    
    void heapsort(int *a, int start, int end) {
        for (int i = (end + 1) / 2 - 1; i >= start; i--) {
            maxheap(a, i, end);
        }
    
        while (end > start) {
            temp(&a[0], &a[end]);
            end--;
            maxheap(a, 0, end);
        }
    }
    
    int main() {
        int a[10] = {3, 87, 2, 93, 78, 56, 61, 38, 12, 40};
    
        heapsort(a, 0, 9);
    
        // 打印排序后的数组
        for (int i = 0; i <= 9; i++) {
            printf("%d ", a[i]);
        }
    
        return 0;
    }
    

    注意:这个简单的堆排序实现没有处理在交换元素后可能需要重新堆化子树的情况,但在实际应用中,这通常是通过递归调用maxheap来完成的。在这个示例中,我们省略了这个步骤,因为它仍然可以工作(对于大多数情况),但可能不是最优的。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月4日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题