我正在学习Cormen的堆排序。
当我试图在阵列上运行heapsort时,出现问题并且程序崩溃(分段错误)。我尝试在heapsort函数中放置一些printf
并打印h->大小和h->计数值,但它们似乎在某种程度上从10变为3(!!!)而没有我触摸它们(尝试在heap_sort和之后的循环之前打印它们)
我真的不明白是什么问题。请帮我。
在windows7上使用Eclipse。
main.c中:
#include <stdio.h>
#include "heap.h"
void print_array2(int *a, int n)
{
int *end = a + n;
while (a < end)
printf("%d ", *a++);
printf("\n");
}
int main(void)
{
int a[] =
{ 4, 1, 3, 2, 16, 9, 10, 14, 8, 7 };
print_array2(a, 10);
heapsort(a, 10);
print_array2(a, 10);
return 0;
}
heap.c:
#include <stdlib.h>
#include <stdio.h>
#include "heap.h"
void heapify(heap *h, int i)
{
int largest, left = LEFT(i), right = RIGHT(i);
if (left < h->count && (*(h->a + left) > *(h->a + i)))
largest = left;
else
largest = i;
if (right < h->count && (*(h->a + right) > *(h->a + largest)))
largest = right;
if (largest != i)
{
swap(h->a + i, h->a + largest);
heapify(h, largest);
}
}
heap *build_heap(int *a, int size)
{
heap h = (heap
)
{ .size = size, .count = size, .a = a };
heap *ph = &h;
int i = size / 2;
while (i >= 0)
heapify(ph, i--);
return ph;
}
void heapsort(int *a, int size)
{
heap *h = build_heap(a, size);
int i;
for (i = h->size - 1; i >= 1; --i)
{
swap(h->a, h->a + i);
h->count--;
heapify(h, 0);
}
}
void print_heap(heap *h)
{
int *end = h->a + h->count, *arr = h->a;
while (arr < end)
printf("%d ", *arr++);
printf("\n");
}
void print_array(heap *h)
{
int *end = h->a + h->size, *arr = h->a;
while (arr < end)
printf("%d ", *arr++);
printf("\n");
}
static void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
heap.h:
#ifndef HEAP_H_
#define HEAP_H_
typedef struct
{
int size; //array size
int count; //heap size
int *a; //int array
} heap;
#define PARENT(x) ((x + 1) / 2)
#define LEFT(x) (2 * (x) + 1)
#define RIGHT(x) (2 * ( (x) + 1) )
void heapify(heap* h, int i);
heap *build_heap(int *a, int size);
void heapsort(int *a, int size);
void print_heap(heap *h);
void print_array(heap *h);
static void swap(int *a, int *b);
#endif /* HEAP_H_ */
答案 0 :(得分:0)
@mafso是正确的。我更改了build_heap以返回堆的副本而不是指针,并且它有效。也许不是最好的方式,但它的工作原理。这是代码:
<强> heap.h 强>
#ifndef HEAP_H_
#define HEAP_H_
typedef struct
{
int size; //array size
int count; //heap size
int *a; //int array
} heap;
#define PARENT(x) ((x + 1) / 2)
#define LEFT(x) (2 * (x) + 1)
#define RIGHT(x) (2 * ( (x) + 1) )
void heapify(heap* h, int i);
heap build_heap(int *a, int size);
void heapsort(int *a, int size);
void print_heap(heap *h);
void print_array(heap *h);
static void swap(int *a, int *b);
#endif /* HEAP_H_ */
<强> heap.c 强>
#include <stdlib.h>
#include <stdio.h>
#include "heap.h"
void heapify(heap *h, int i)
{
int largest, left = LEFT(i), right = RIGHT(i);
if (left < h->count && (*(h->a + left) > *(h->a + i)))
largest = left;
else
largest = i;
if (right < h->count && (*(h->a + right) > *(h->a + largest)))
largest = right;
if (largest != i)
{
swap(h->a + i, h->a + largest);
heapify(h, largest);
}
}
heap build_heap(int *a, int size)
{
heap h;// = (heap) { .size = size, .count = size, .a = a };
h.size = size;
h.count = size;
h.a = a;
heap *ph = &h;
int i = size / 2;
while (i >= 0)
heapify(ph, i--);
return *ph;
}
void heapsort(int *a, int size)
{
heap h = build_heap(a, size);
int i;
for (i = h.size - 1; i >= 1; --i)
{
swap(h.a, h.a + i);
h.count--;
heapify(&h, 0);
}
}
void print_heap(heap *h)
{
int *end = h->a + h->count, *arr = h->a;
while (arr < end)
printf("%d ", *arr++);
printf("\n");
}
void print_array(heap *h)
{
int *end = h->a + h->size, *arr = h->a;
while (arr < end)
printf("%d ", *arr++);
printf("\n");
}
static void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
这是我的输出:
4 1 3 2 16 9 10 14 8 7
1 2 3 4 7 8 9 10 14 16