-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapsort.c
61 lines (59 loc) · 1.04 KB
/
heapsort.c
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
void insert(int A[],int num,int pos)
{
int m,item;
A[pos]=num;
m=pos;
item=A[pos];
while(m>0 && A[(m-1)/2]<A[m])
{
A[m]=A[(m-1)/2];
m=(m-1)/2;
A[m]=item;
}
}
void heapify(int A[],int j,int a)
{
int number;
int largest=a;
int l=2*a+1;
int r=2*a+2;
if(l<=j && A[l]>A[largest])
largest=l;
if(r<=j && A[r]>A[largest])
largest=r;
if(largest!=a)
{
number=A[a];
A[a]=A[largest];
A[largest]=number;
heapify(A,j,largest);
}
}
int main()
{
int A[10]={0};
insert(A,80,0);
insert(A,11,1);
insert(A,25,2);
insert(A,79,3);
insert(A,8,4);
insert(A,32,5);
insert(A,41,6);
int i;
for(i=0;i<7;i++)
printf("%d ",A[i]);
i=6;
while(i>0)
{
int num;
num=A[i];
A[i]=A[0];
A[0]=num;
i=i-1;
heapify(A,i,0);
}
printf("\n");
for(i=0;i<7;i++)
printf("%d ",A[i]);
}