Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
堆排序C-输出不正确_C_Arrays_Sorting_Heapsort - Fatal编程技术网

堆排序C-输出不正确

堆排序C-输出不正确,c,arrays,sorting,heapsort,C,Arrays,Sorting,Heapsort,我正在尝试对一个由10个元素组成的数组按升序实现堆排序。 我正在遵循这些步骤- 堆\u排序(arr、大小\u数组): build_max_heap(arr) for(parent=size_array to 1): swap(arr[1],arr[parent]) size_array = size_array - 1; max_heapify(arr,1,size); 但我的输出完全搞砸了。 我不知道我错在哪里 这是我的意见- 20 15

我正在尝试对一个由10个元素组成的数组按升序实现堆排序。 我正在遵循这些步骤-

堆\u排序(arr、大小\u数组):

 build_max_heap(arr)
    for(parent=size_array to 1):
       swap(arr[1],arr[parent])
       size_array = size_array - 1;
       max_heapify(arr,1,size);
但我的输出完全搞砸了。 我不知道我错在哪里

这是我的意见-

20  15  10  1   15  9   2   6   7   9
我的生成\u最大\u堆输出-

20  15  10  7   15  9   2   6   1   9
20  15  10  7   15  9   2   6   1   9
我的排序数组与build\u max\u堆输出相同-

20  15  10  7   15  9   2   6   1   9
20  15  10  7   15  9   2   6   1   9
我做错了什么

这是我的密码:

    void max_heapify(int *arr,int i,int size)
{
    //i is the index of parent node. 2i is left child, 2i+1 is right
    int left = (2*i)+1;
    int right = (2*i)+2;
    int max;
    int temp;

    //check which node is the max, parent or one of its children. store max idx.
    if ((left<=size)&&(arr[left]>arr[i]))
        max = left;
    else
        max = i;
    if ((right<=size)&&(arr[right]>arr[max]))
        max = right;

    //swap parent with max.
    if(max!=i)
    {
        temp = arr[max];
        arr[max]=arr[i];
        arr[i]=temp;
        max_heapify(arr,max,size);
    }

}

void build_max_heap(int *arr,int size)
{
    for(int i = size/2; i>=0; i--)
    {
        max_heapify(arr,i,size);
    }
}

void heap_sort(int *arr, int size)
{
    int temp;
    build_max_heap(arr,size);
    int i = size;
    while(size>0)
    {
        //swap
        temp = arr[i];
        arr[i] = arr[0];
        arr[0] = temp;
        //reduce size
        size = size -1;
        //heapify
        max_heapify(arr,0,size);

    }
}
void max_heapify(int*arr,int i,int size)
{
//i是父节点的索引,2i是左子节点,2i+1是右子节点
int left=(2*i)+1;
右整数=(2*i)+2;
int max;
内部温度;
//检查哪个节点是max、父节点或其子节点之一。存储max idx。
if((leftarr[i]))
最大值=左;
其他的
max=i;
如果((rightarr[max]))
最大值=右侧;
//用max替换父对象。
如果(最大!=i)
{
温度=arr[最大值];
arr[max]=arr[i];
arr[i]=温度;
最大值(arr、最大值、尺寸);
}
}
无效生成\u最大\u堆(int*arr,int size)
{
对于(int i=size/2;i>=0;i--)
{
最大值(arr、i、尺寸);
}
}
无效堆排序(int*arr,int size)
{
内部温度;
构建最大堆(arr,大小);
int i=大小;
而(大小>0)
{
//交换
温度=arr[i];
arr[i]=arr[0];
arr[0]=温度;
//缩小尺寸
尺寸=尺寸-1;
//希皮菲
最大值(arr,0,大小);
}
}

您的代码多次访问元素
arr[size]
,这是超出有效范围的一项,
0 arr[i])
最大值=左;
其他的
max=i;
如果(右arr[max])
最大值=右侧;
如果(最大!=i){
int temp=arr[max];
arr[max]=arr[i];
arr[i]=温度;
最大值(arr、最大值、尺寸);
}
}
无效生成\u最大\u堆(int*arr,int size)
{
int i=大小/2;
而(我--){
最大值(arr、i、尺寸);
}
}
无效堆排序(int*arr,int size)
{
构建最大堆(arr,大小);
而(大小--){
内部温度=arr[尺寸];
arr[size]=arr[0];
arr[0]=温度;
最大值(arr,0,大小);
}
}

您的代码多次访问元素
arr[size]
,这是超出有效范围的一项,
0 arr[i])
最大值=左;
其他的
max=i;
如果(右arr[max])
最大值=右侧;
如果(最大!=i){
int temp=arr[max];
arr[max]=arr[i];
arr[i]=温度;
最大值(arr、最大值、尺寸);
}
}
无效生成\u最大\u堆(int*arr,int size)
{
int i=大小/2;
而(我--){
最大值(arr、i、尺寸);
}
}
无效堆排序(int*arr,int size)
{
构建最大堆(arr,大小);
而(大小--){
内部温度=arr[尺寸];
arr[size]=arr[0];
arr[0]=温度;
最大值(arr,0,大小);
}
}

在堆中_sort
i
应设置为循环内的大小。按照您的方式,您每次都将arr[0]与arr[9]交换。相反,每次大小减少1时,arr[0]应与arr[size]交换

int i = size; //Here is your main problem
while(size>0)
{
    //swap
    temp = arr[i];
    arr[i] = arr[0];
    arr[0] = temp;
    //reduce size
    size = size -1;
    //heapify
    max_heapify(arr,0,size);

}

在堆中,排序
i
应设置为循环内的大小。按照您的方式,您每次都将arr[0]与arr[9]交换。相反,每次大小减少1时,arr[0]应与arr[size]交换

int i = size; //Here is your main problem
while(size>0)
{
    //swap
    temp = arr[i];
    arr[i] = arr[0];
    arr[0] = temp;
    //reduce size
    size = size -1;
    //heapify
    max_heapify(arr,0,size);

}

在调试器中逐行遍历代码以查看其是否按预期工作。在调试器中逐行遍历代码以查看其是否按预期工作。