数据结构与算法--堆排序问题
本帖最后由 zlQhs 于 2015-3-28 18:11 编辑有一个值有问题
代码如下:
#include "stdafx.h"
#include <iostream>
#define DATA_SIZE 14
int iCompareCount = 0;
void swap(int data[], int i, int j)
{
int iTemp = data;
data = data;
data = iTemp;
}
void heapAdjust(int data[], int s, int iDataSize)
{
int iTemp = data;
for ( int i = 2 * s; i <= iDataSize; i *= 2)
{
iCompareCount++;
if ( i < iDataSize && data < data )
{
i++;
}
if ( iTemp >= data )
{
break;
}
data = data;
s = i;
}
data = iTemp;
}
void heapSort(int data[], int iDataSize)
{
int i = 0;
for ( i = iDataSize / 2; i > 0; i-- )
{
heapAdjust(data, i, iDataSize);
}
for ( i = iDataSize; i > 1; i-- )
{
swap(data, 1, i);
heapAdjust(data, 1, i - 1);
}
}
void printResult(int data[], int iDataSize)
{
printf("排序后的结果是:");
for ( int i = 0; i < iDataSize; i++)
{
printf("%d ", data);
}
printf("\n\n");
}
int _tmain(int argc, _TCHAR* argv[])
{
int data = {3,6,1,4,2,5,9,33,8,1,22,32,11,18};
heapSort(data, DATA_SIZE);
printResult(data, DATA_SIZE);
getchar();
return 0;
}
运行结果:
页:
[1]