代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void HeapAdjust(HeapType &H, int s, int m)
{
RedType rc = H.r[s];
int j;
for (j = 2 * s; j <= m; j *= 2)
{
if (j < m&<(H.r[j].key, H.r[j + 1].key))
++j;

if (!LT(rc.key, H.r[j].key))
break;
H.r[s] = H.r[j];
s = j;
}
H.r[s] = rc;
}

//对顺序表H进行堆排序
void HeapSort(HeapType &H)
{
int i;
//建立最大堆
for (i = H.length / 2; i >=1; --i)
HeapAdjust(H, i, H.length);

for (i = H.length; i >=2; --i)
{
RedType temp = H.r[1];
H.r[1] = H.r[i];
H.r[i] = temp;

HeapAdjust(H, 1, i - 1);
}
}