Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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 分段错误:11,当我测试书中的heapsort代码时';算法简介';_C_Algorithm_Sorting_Heapsort - Fatal编程技术网

C 分段错误:11,当我测试书中的heapsort代码时';算法简介';

C 分段错误:11,当我测试书中的heapsort代码时';算法简介';,c,algorithm,sorting,heapsort,C,Algorithm,Sorting,Heapsort,我用C语言编写了《算法介绍》一书中heapsort的伪代码,出现了一个错误:分段错误:11。内存溢出有问题吗 #define LEFT(i) 2*i; #define RIGHT(i) 2*i+1; #define PARENT(i) i/2; /************************************************/ /*heap_sort -- stable *In order to easily determine the relationship of ind

我用C语言编写了《算法介绍》一书中heapsort的伪代码,出现了一个错误:分段错误:11。内存溢出有问题吗

#define LEFT(i) 2*i;
#define RIGHT(i) 2*i+1;
#define PARENT(i) i/2;

/************************************************/
/*heap_sort -- stable
*In order to easily determine the relationship of index between parent node and child nodes,
*available index of arr starts from 1.
*/
/************************************************/

/*
    Available index of arr starts from 1.
    Length represents the last element's index.
    Heap_size is biggest range in arr.
*/
typedef struct {
    int heap_size;
    int length;
    int *arr;
} heap;

/*
    Node i's left_child tree and right_child tree are all max heap.
    This function put node i's value into proper position in oder to keep a max heap.
*/
void max_heapify(heap h, int i) {
    int *arr = h.arr;
    int left = LEFT(i);
    int right = RIGHT(i);
    int largest = i;

    if (arr[left] > arr[i] && left <= h.heap_size)
        largest = left;
    if (arr[right] > arr[largest] && right <= h.heap_size)
        largest = right;

    if (largest != i) {
        exchange(h.arr + i, h.arr + largest);
        max_heapify(h, largest);
    }

    return;
}

/*
    Build a max heap.
*/
void build_max_heap(heap h) {
    h.heap_size = h.length;
    for (int i = h.length / 2; i > 0; i--)  //leaf nodes need not to call max_heapify
        max_heapify(h, i);
    return;
}

void heap_sort(heap h) {
    build_max_heap(h);
    int *arr = h.arr;
    for (int i = h.length; i > 1; i--) {
        exchange(arr + 1, arr + i);
        h.heap_size--;
        max_heapify(h, 1);
    }
    return;
}

int main() {
    int arr[] = {1,4,9,0,2,1,6,2};
    int num = sizeof(arr) / sizeof(int);
    heap h = {0, num - 1, arr};
    heap_sort(h);
    for (int i = 1; i < num; i++) {
        printf("%d,", arr[i]);
        }

    return 0;
}
#定义左(i)2*i;
#定义权利(i)2*i+1;
#定义父(i)i/2;
/************************************************/
/*堆排序——稳定
*为了方便地确定父节点和子节点之间的索引关系,
*arr的可用索引从1开始。
*/
/************************************************/
/*
arr的可用索引从1开始。
长度表示最后一个元素的索引。
堆大小是arr中的最大范围。
*/
类型定义结构{
int堆大小;
整数长度;
int*arr;
}堆;
/*
节点i的左_子树和右_子树都是max-heap。
此函数将节点i的值放入适当的位置,以保持最大堆。
*/
void max_heapify(堆h,堆i){
int*arr=h.arr;
int左=左(i);
int right=right(i);
int=i;
if(arr[left]>arr[i]&&left-arr[max]&&right 0;i--)//叶节点不需要调用max_heapify
max_heapify(h,i);
回来
}
无效堆\排序(堆h){
构建最大堆(h);
int*arr=h.arr;
对于(int i=h.length;i>1;i--){
交换(arr+1,arr+i);
h、 堆大小--;
max_heapify(h,1);
}
回来
}
int main(){
int arr[]={1,4,9,0,2,1,6,2};
int num=sizeof(arr)/sizeof(int);
堆h={0,num-1,arr};
堆排序(h);
for(int i=1;i

最大堆是一个二叉树,其中节点的值大于其左、右子节点的值

max_heapify()
函数中,首先需要检查
left
right
是否仍在边界内-即它们是
@2501此处是信息。bootstrap.sh:第10行:23179分段错误:11“$A_OUT”左、右等的定义在哪里?
max_heapify
:它需要递归调用的结束条件。#定义左(i)2*i#定义权利(i)2*i+1#定义父(i)i/2;“左”表示它的左子节点。@samgakYou是右的,我应该加上“max_heapify(h,max);”进入上面的{}。谢谢。@bluepixy非常感谢,你完美地解决了我的问题。
void max_heapify(heap h, int i) {
    int *arr = h.arr;
    int left = LEFT(i);
    int right = RIGHT(i);
    int largest = i;

    /* ISSUE: You are evaluating first, checking bounds second */   
    /* if (arr[left] > arr[i] && left <= h.heap_size) */ 

    /* Right way: Check bounds first, evaluate second */
    if (left <= h.heap_size && arr[left] > arr[i]) /* Right */
        largest = left;

    /* ISSUE: You are evaluating first, checking bounds second */ 
    /* if (arr[right] > arr[largest] && right <= h.heap_size) */

    /* Right way: Check bounds first, evaluate second */
    if (right <= h.heap_size && arr[right] > arr[largest])
        largest = right;

    if (largest != i) {
        exchange(h.arr + i, h.arr + largest);
        max_heapify(h, largest);
    }

    return;
}
void heap_sort(heap h)
{
    build_max_heap(h);
    int *arr = h.arr;
    h.heap_size = h.length; /* This statement REQUIRED here */
    for (int i = h.length; i > 1;  i--)
    {
            exchange(arr + 1, arr + i); 
            h.heap_size = h.heap_size - 1;
            max_heapify(h, 1); 
    }

    return;
}