}
37 int heapify(int *array,int pos,int len)
38 {
39
40 int maxpos = findmaxpos(array,pos,len);
41
42
43 if(pos != maxpos)
44 {
45 exchange(&array[pos],&array[maxpos]);
46 heapify(array,maxpos,len);
47
48 }
49 return 0;
50 }
51
52 int buildheap(int *array,int len)
53 {
54 int i ;
55 for(i = (len/2);i>0;i--)
56 {
57 heapify(array,i,len);
58
59 }
60 return 0;
61 }
62
63 int heapsort(int *array,int len)
64 {
65 int i;
66
67 buildheap(array,len);
68 for (i = len;i>1;i--)
69 {
70 exchange(&array[1],&array[i]);
71 len--;
72 heapify(array,1,len);
73 }
74 return 0;
75 }
76
77
78 int test_heapsort()
79 {
80 int array[11] = {0,16,14,10,8,7,9,3,2,4,1};
81 int i;
82 for(i = 1;i<=10;i++)
83 {
84 printf("%d\t",array[i]);
85 }
86 printf("\n");
87
88 heapsort(array,10);
89 for(i = 1;i<=10;i++)
90 {
91 printf("%d\t",array[i]);
92 }
93 printf("\n");
94 return 0;
95
96 }
97 int test_buildheap()
98 {
99 int array[11] = { 0 , 4, 1,3,2,16,9,10,14,8,7};
100
101 buildheap(array,10);
102
103 assert(array[1] == 16);
104 assert(array[5] == 7);
105 assert(array[7]== 3);
106 assert(array[9] = 2);
107
108 return 0;
109 }
110
111 int test_heapify()
112 {
113 int array[11] = {0,16,4,10,14,7,9,3,2,8,1};
114 heapify(array,2,10);
115
116 assert(array[1] = 16);
117
118 assert(array[2] = 14);
119
120 assert(array[4] = 8);
121
122 assert(array[8] = 2);
123
124 return 0;
125 }
126 int test_findmaxpos()
127 {
128 int array[5] = {0,1,8, 4, 3};
129 int a = findmaxpos(array,1,4);
130 assert (a == 2);
131 return 0;
132 }
133 int test_exchange()
134 {
int a = 3;
136 int b = 5;
137 exchange(&a,&b);
138 assert(a == 5 );
139 assert(b == 3);
140 return 0;
141 }
142 int test()
143 {
144 test_exchange();
145 test_findmaxpos();
146 test_heapify();
147 test_buildheap();
148 test_heapsort();
149 return 0;
150
151 }
152 int main(int argc,char *argv[])
153 {
154 test();
155 return 1;
156 }