我正在用 C 语言实现经典的堆排序算法。我已经盯着这段代码看了好几个小时,但仍然无法弄清楚我这辈子出了什么问题。输入 3 1 2 7 4 0 2 运行良好并产生正确的堆,但是当我在末尾添加 8 时(并将大小增加一)。它不再产生堆。有任何想法吗?我认为这只是一个错误的愚蠢行为。供参考http://en.wikipedia.org/wiki/Heapsort
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
#define MAX_ARR 1024
void build_heap(int * fn, int len);
void heapify(int *f, int n, int size);
void verify_relations(int *f, int n);
void print_nums (int n[], int len);
void swap(int * a, int * b);
void heap_sort(int * a, int len);
int main(int argc, char **argv){
/* input works -- 3 1 2 7 4 0 2 */
/* input broken -- 3 1 2 7 4 0 2 8 */
int nums[100] = { 3, 1, 2, 7, 4, 0, 2, 8 };
int len = 8;
build_heap(nums, len);
verify_relations(nums, len);
print_nums(nums, len);
return 0;
}
void heap_sort(int nums[], int len){
int i;
build_heap(nums, len);
for (i = len; i > 0; --i)
{
swap(&nums[1], &nums[i]);
heapify(nums, 1, len);
}
}
void build_heap(int * nums, int len) {
int size = len, i;
for (i = size; i >= 0; i--){
heapify(nums, i, size);
}
}
void heapify(int f[], int n, int size){
int left = 2 * n,
right = 2 * n + 1,
largest = 0;
if (left < size && f[left] >= f[n])
largest = left;
else
largest = n;
if (right < size && f[right] >= f[largest])
largest = right;
if (largest != n) {
swap(&f[n], &f[largest]);
heapify(&n, largest, size);
}
}
void swap(int * a, int * b){
int temp = *a;
*a = *b;
*b = temp;
}
void verify_relations(int a[], int n){
if (a[0] >= a[1] && a[0] >= a[2])
printf("True - '%d >= %d && %d >= %d' \n", a[0], a[1], a[0], a[2]);
else
printf("False\n");
if (a[1] >= a[3] && a[1] >= a[4])
printf("True - '%d >= %d && %d >= %d' \n", a[1], a[3], a[1], a[4]);
else
printf("False\n");
if (a[2] >= a[4] && a[2] >= a[5])
printf("True - '%d >= %d && %d >= %d' \n", a[2], a[4], a[3], a[5]);
else
printf("False\n");
}
void print_nums (int n[], int len) {
int c;
for (c = 0; c < len; c++){
printf("%d ", n[c]);
}
}