首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大神帮忙见见

2013-03-20 
大神帮忙看看/* Name: H

大神帮忙看看
/*
 ============================================================================
 Name        : HeapSort.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

int g_array[8] = {19, 33, 4, 23, 8, 5, 14, 20};

void HeadAdjust(int * array, int heap_top, int heap_last);

void HeapSort(int * array, int length);

int main(void)
{
int i;
HeapSort(g_array, 8);
for (i = 0; i < 8; i++)
printf("  %d,  ", g_array[i]);
return EXIT_SUCCESS;
}

void HeapSort(int * array, int length)
{
int i;
int temp;
for(i = length / 2; i > 0; i--) // 建立大顶堆
HeadAdjust(array, i, length);
for(i = 0; i < length; i++){
temp = array[0];  // 交换第一个和最后一个
array[0] = array[length - i - 1];
array[length - i - 1] = temp;
HeadAdjust(array, 0, length - i -1);
}
}

//重新建立大顶堆
void HeapAdjust(int * array, int heap_top, int heap_last)
{
int i;
int store = array[heap_top];
for(i = heap_top * 2; i < heap_last; i *= 2){
if (array[i] > array[i + 1])
i++;
if (array[heap_top] > array[i])
break;
array[i] = array[heap_top];
heap_top = i;
}
array[heap_top] = store;
}

编译后说
undefined reference to `HeadAdjust'
什么情况??????
[解决办法]

/*
============================================================================
Name        : HeapSort.c
Author      : 
Version     :
Copyright   : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/

#include <stdio.h>
#include <stdlib.h>

int g_array[8] = {19, 33, 4, 23, 8, 5, 14, 20};

void HeapAdjust(int * array, int heap_top, int heap_last);

void HeapSort(int * array, int length);

int main(void)
{
int i;
HeapSort(g_array, 8);
for (i = 0; i < 8; i++)
printf("  %d,  ", g_array[i]);
return EXIT_SUCCESS;
}

void HeapSort(int * array, int length)
{
int i;
int temp;
for(i = length / 2; i > 0; i--) // 建立大顶堆
HeapAdjust(array, i, length);


for(i = 0; i < length; i++){
temp = array[0];  // 交换第一个和最后一个
array[0] = array[length - i - 1];
array[length - i - 1] = temp;
HeapAdjust(array, 0, length - i -1);
}
}

//重新建立大顶堆
void HeapAdjust(int * array, int heap_top, int heap_last)
{
int i;
int store = array[heap_top];
for(i = heap_top * 2; i < heap_last; i *= 2){
if (array[i] > array[i + 1])
i++;
if (array[heap_top] > array[i])
break;
array[i] = array[heap_top];
heap_top = i;
}
array[heap_top] = store;
}


HeapAdjust不要写成HeadAdjust就行了。
[解决办法]
copy要仔细

热点排行