📄 11-7.c
字号:
#include < stdio.h>
#define MAX_SIZE 1000 /* maximum size of list plus one */
typedef struct {
int key;
/*其他数据域*/
}element;
void adjust(element list[ ] , int root, int n)
{
int child,rootkey;
element temp;
temp = list[root];
rootkey = list[root].key;
child = 2*root;
while (child <= n){
if ((child < n) && (list[child].key < list[child+1].key))
child++;
if (rootkey > list[child].key)
break;
else{
list[child/2] = list[child];
child *= 2;
}
}
list [child/2] = temp;
}
void Swap(element *a,element *b)
{
element temp;
temp=*a;
*a=*b;
*b=temp;
}
void heapsort(element list[ ], int n)
{
int i,j;
for (i = n/2; i > 0; i--) /*建立max heap*/
adjust(list, i, n);
for (i = n-1; i > 0; i--){ /*排序*/
Swap(&list[1],&list[i+1]);
adjust(list, 1, i);
}
}
void main()
{
element list[MAX_SIZE];
heapsort(list,MAX_SIZE-1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -