-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheap_sort.c
More file actions
54 lines (54 loc) · 798 Bytes
/
Copy pathheap_sort.c
File metadata and controls
54 lines (54 loc) · 798 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int a[10],n1=10,b[10],n2=9;
void create_heap(int j,int n){
int c;
b[j]=n;
while(j>0){
if ((b[(j-1)/2])<b[j]){
c=b[j];
b[j]=b[(j-1)/2];
b[(j-1)/2]=c;
}
j=(j-1)/2;
}
}
int heapify(){
int j=0,c;
while((2*j+1<n2)&&((b[j]<b[(2*j+1)])||(b[j]<b[(2*j+2)]))){
if (b[(2*j+1)]>b[2*j+2]){
c=b[j];
b[j]=b[2*j+1];
b[2*j+1]=c;
j=2*j+1;
}
else{
c=b[j];
b[j]=b[2*j+2];
b[2*j+2]=c;
j=2*j+2;
}
}
return b[0];
}
void main(){
int i,d,m;
for (i=0;i<n1;i++){
scanf("%d",&a[i]);
create_heap(i,a[i]);
}
for (i=0;i<n1;i++)
printf("%d ",b[i]);
d=n1;
a[d-1]=heapify();
for (i=n1-1;i>0;i--){
m=b[i];
b[i]=b[n2-i];
b[n2-i]=m;
n2--;
a[i-1]=heapify();
}
printf("\n");
for (i=0;i<n1;i++)
printf("%d ",a[i]);
}